在用一台新设备操纵selenium模拟浏览器时:
设备条件:安装Anaconda(或Python),配置有WLAN(以及vpn)。
pip install -U selenium
from selenium import webdriver
第一步:配置Webdriver
在谷歌浏览器直接搜索 chrome://version/ 查看浏览器版本。
在这个地址 :http://chromedriver.storage.googleapis.com/index.html 下载与浏览器版本相匹配的driver。
手动将webdriver放在python运行路径下,以及浏览器路径下。
#anaconda路径:
/opt/anaconda3/bin/
C:\Users\Administrator\anaconda3\Scripts\
#浏览器可执行文件路径:
/Applications/Google Chrome.app/Contents/MacOS/ #具体参考浏览器版本的说明
第二步:配置Tmp文件(选择)
找到浏览器版本页面的个人资料文件,创建空的.tmp文件。
option = webdriver.ChromeOptions()
option.add_argument(r"user-data-dir=配置文件路径")
driver = webdriver.Chrome(executable_path = '/opt/anaconda3/bin/chromedriver',options=option)
配置文件可以缓存登录信息,每开启一个新的模拟浏览器,通过上述代码手动登入账号(或通过代码自动登入)。之后不用重复登录即可爬取文章内容。注意:浏览器设置不要清除缓存。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理。