python 模拟浏览器访问网页 selenium+chromedriver+360浏览器

要模拟浏览器访问网页,网上较普遍的是用selenium+chromedriver+chrome浏览器。

 一,安装selenium第三方库

在cmd命令行串口输入pip install selenium

二,安装webdriver

网上主要有三类浏览器,chrome和firefox和ie,我习惯用360安全浏览器,它采用的是chrome内核。

下载chromedriver,需要与浏览器的版本相对应。我浏览器的版本是63,对应的chromedriver版本是2.36,到下面的地址下载对应的版本。

http://chromedriver.storage.googleapis.com/index.html

将chromedriver.exe所在目录加入到windows的环境变量path中

三,输入下面代码

from selenium import webdriver
browser = webdriver.Chrome()
browser.get('http://www.baidu.com/')  

倒霉,运行出错,主要提示如下:

cannot find Chrome binary

原因分析:大概意思就是chrome浏览器未找到,经过网上大量找资料,回头终于发现,只要将启动浏览器的启动exe文件改个名就行,即将360se.exe改为chrome.exe即可。

这样,程序就可以驱动浏览器了。

也可以这样: 

from selenium import webdriver
option=webdriver.ChromeOptions()
option.binary_location=r'C:\360se\360se6\Application\360se.exe'
drive=webdriver.Chrome(r'C:\360se\360se6\Application\chromedriver.exe',options=option)
drive.get('http://www.lcez.com.cn')

 四,注:上面是普通模式启动浏览器,还有另外一种 静默模式,即不显示浏览器界面。

option.add_argument('--headless')

  在这里出现了错误

Message: session not created exception: Chrome version must be >= 62.0.3202.0

原因未知,但换成chrome浏览器,则正常, 如下:

option=webdriver.ChromeOptions()
option.add_argument('--headless')
drive=webdriver.Chrome(options=option)
drive.get('http://lcez.com.cn')
print(drive.title)
drive.quit()

  

posted @ 2019-06-24 17:57  隐泉  阅读(5282)  评论(0编辑  收藏  举报