1.chromedriver与chrome的对应关系表
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 |
国内镜像下载地址:https://npm.taobao.org/mirrors/chromedriver/
下载完成之后解压文件,将chromedriver.exe直接放到python安装目录下的Scripts文件夹下。
2.代码实现
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()
如果能正常打开网页,那么就说明selenium成功了。
3.数据爬取
可以采用selenium的xpath方式,具体方法参考xpath
browser.find_element_by_xpath('//input[@id="kw"]')
4.异常错误
错误1:selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary
1.指定chromedriver.exe驱动绝对路径
driver = webdriver.Chrome(r'd:\xxx\chromedriver.exe')
2.添加chrome.exe到系统path环境变量
3.在代码中指定chrome.exe绝对路径。设置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')
错误2:Only local connections are allowed错误
貌似这不影响使用,但网上一般说是版本兼容问题。
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.