python + selenium + chrome上手指南

1.chromedriver與chrome的對(duì)應(yīng)關(guān)系表

chromedriver版本 支持的Chrome版本
v2.46 v71-73
v2.45 v70-72
v2.44 v69-71
v2.43 v69-71
v2.42 v68-70
v2.41 v67-69
v2.40 v66-68
v2.39 v66-68
v2.38 v65-67
v2.37 v64-66
v2.36 v63-65
v2.35 v62-64
v2.34 v61-63
v2.33 v60-62
v2.32 v59-61
v2.31 v58-60
v2.30 v58-60
v2.29 v56-58
v2.28 v55-57
v2.27 v54-56
v2.26 v53-55
v2.25 v53-55
v2.24 v52-54
v2.23 v51-53
v2.22 v49-52
v2.21 v46-50
v2.20 v43-48
v2.19 v43-47
v2.18 v43-46
v2.17 v42-43
v2.13 v42-45
v2.15 v40-43
v2.14 v39-42
v2.13 v38-41
v2.12 v36-40
v2.11 v36-40
v2.10 v33-36
v2.9 v31-34
v2.8 v30-33
v2.7 v30-33
v2.6 v29-32
v2.5 v29-32
v2.4 v29-32

國(guó)內(nèi)鏡像下載地址:https://npm.taobao.org/mirrors/chromedriver/

下載完成之后解壓文件,將chromedriver.exe直接放到python安裝目錄下的Scripts文件夾下。

2.代碼實(shí)現(xiàn)

from selenium import webdriver

option = webdriver.ChromeOptions()
option.binary_location=r'xxxx\Chrome.exe' 
browser = webdriver.Chrome(chrome_options=option)
browser.get("www.baidu.com")
print(browser.page_source)
browser.close()

如果能正常打開網(wǎng)頁,那么就說明selenium成功了。

3.數(shù)據(jù)爬取

可以采用selenium的xpath方式,具體方法參考xpath
browser.find_element_by_xpath('//input[@id="kw"]')

4.異常錯(cuò)誤

錯(cuò)誤1:selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
1.指定chromedriver.exe驅(qū)動(dòng)絕對(duì)路徑

driver = webdriver.Chrome(r'd:\xxx\chromedriver.exe')
2.添加chrome.exe到系統(tǒng)path環(huán)境變量

3.在代碼中指定chrome.exe絕對(duì)路徑。設(shè)置binary_location屬性

option = webdriver.ChromeOptions()
option.binary_location=r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe' 
driver = webdriver.Chrome(chrome_options=option)
driver.get('https://www.baidu.com')

錯(cuò)誤2:Only local connections are allowed錯(cuò)誤
貌似這不影響使用,但網(wǎng)上一般說是版本兼容問題。

This [info] log message conveys that the ChromeDriver binary will only accept connections from the local machine.

As most of the driver implementations (GeckoDriver , IEDriverServer and ChromeDriver) creates an HTTP Server and the Selenium Clients (Java, Python, C#, NodeJS) all uses a JSON-over-HTTP protocol to communicate with the WebDriver and automates the Browser Client. As the HTTP server is only listening on an open port for HTTP requests generated by the client language bindings, connections to the HTTP server started by the client language bindings are restricted to only be allowed to come from the same processes on the same host. This limitation does not apply to connections the browser can make to third-party associated websites, rather it simply prevents incoming connections from other websites.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容