爬虫工具之selenium(一)

372次阅读
没有评论

使用selenium要经过三大步骤,一是下载浏览器,二是下载selenium,三是下载浏览器对应的驱动

下载浏览器忽略

一、下载selenium
下载selenium-3.141.0-py2.py3-none-any
官方文档

二、下载浏览器对应驱动driver
(一)下载
1、下载chormedriver(对应谷歌浏览器)http://npm.taobao.org/mirrors/chromedriver/
否则会出现类似如下报错信息:
selenium.common.exceptions.WebDriverException: Message: Service C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe unexpectedly exited. Status code was: 0
2、下载geckodriver(对应Firefox)
https://github.com/mozilla/geckodriver/releases
3、下载IEdriver(对应Ie浏览器)
http://selenium-release.storage.googleapis.com/index.html
IEDriverServer的版本号和Selenium的版本号一定要一致,查看Selenium版本:
爬虫工具之selenium(一)
因此选用3.141版本的IEDriverServer

(二)具体安装过程
以Chrome为例
具体安装过程参考:如何快速下载、安装和配置chromedriver ?

1.查到安装的chrome版本:
爬虫工具之selenium(一)
2.对照Chrome版本与chromedriver兼容版表,找到对应的chromedriver并下载:
爬虫工具之selenium(一)
3.将解压后的chromedriver.exe扔到chrome安装目录:
爬虫工具之selenium(一)
4.最后在环境变量中添加路径:
爬虫工具之selenium(一)
添加路径后运行代码依然报错:
Message: ‘chromedriver’ executable needs to be in PATH. Please see https://sites.google.com/a/chromium.org/chromedriver/home
查证后发现还需要将chromedriver.exe拷贝到python安装目录下
爬虫工具之selenium(一)
总结:找到对应版本下载,将chromedriver.exe , geckodriver.exe , Iedriver.exe扔到对应浏览器和python安装目录,然后再将浏览器的安装目录添加到系统环境变量的Path下面。

顺便记录一下调试代码中出现的其他错误:
1、SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: tr

chrome_driver=('C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe') browser = webdriver.Chrome(executable_path=chrome_driver)

问题描述:转义有问题
解决方法参考:运行python文件报SyntaxError: (unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: tr
个人比较倾向于在路径前面加r,即保持字符原始值的意思,以后还会经常用到
修改后的代码:

chrome_driver=(r'C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe') browser = webdriver.Chrome(executable_path=chrome_driver)

神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:Python教程2022-10-24发表,共计1611字。
新手QQ群:570568346,欢迎进群讨论 Python51学习