问题一:当使用selenium+chromedriver进行用户模拟登陆的时候,服务器会检测出为非人工操作,从而不让访问。
处理办法:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
# 配置浏览器参数,规避人机检测
option = ChromeOptions()
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("--no-sandbox")
option.add_argument("--lang=zh-CN")
b = webdriver.Chrome(options=option)
b.get('https://xxx.com/')
print(b.page_source)
问题二:某些情况下,需要使用自己常用配置的Chrome浏览器(通过代码驱动打开的是一个新的浏览器用户窗口,类似隐私窗口,当想用自己登录了账号,安装了插件的Chrome浏览器时,可以按以下配置使用)
处理办法:
from selenium import webdriver
from selenium.webdriver import ChromeOptions
option = ChromeOptions()
# 指定配置文件运行
option.add_argument("--user-data-dir="+r"C:/Users/windows/AppData/Local/Google/Chrome/User Data/")
# 过人机检测
option.add_experimental_option('excludeSwitches', ['enable-automation'])
option.add_argument("--no-sandbox")
option.add_argument("--lang=zh-CN")
# Headless模式运行 --无头模式运行
# option.add_argument('--headless')
brower = webdriver.Chrome(options=option)