selenium使用已打开的chrome浏览器绕过验证,亲测可用
首先说明下这样做的好处:就是你不用验证登陆状态,可以先登陆,再爬虫
1.先打开windows cmd 进入chrome安装目录,一般在C:\Program Files (x86)\Google\Chrome\Application下,然后运行
chrome.exe --remote-debugging-port=9222--user-data-dir="C:\selenum\AutomationProfile"
就打开了一个chrome浏览器,后面就是操作这个浏览器了,你可以先登陆,然后cookie,token什么的都有了
2.代码实现
# -*- coding: utf-8 -*-
fromseleniumimportwebdriver
fromselenium.webdriver.chrome.optionsimportOptions
chrome_options = Options()
chrome_options.add_experimental_option("debuggerAddress","127.0.0.1:9222")
chrome_driver ="C:\Program Files (x86)\Google\Chrome\Application\chromedriver.exe"
driver = webdriver.Chrome(chrome_driver, chrome_options=chrome_options)
driver.get('https://baidu.com')
chrome_driver 这个路径要根据 自己的安装路径来,注意这里chromedriver.exe 要去下载一个,给出淘宝的镜像吧,国外的要翻墙;还有端口要一致,可以随便定义未使用的端口
https://npm.taobao.org/mirrors/chromedriver
IE:
http://selenium-release.storage.googleapis.com/index.html