selenium操作浏览器的一些配置
#设置用户代理
USER_AGENTS = [
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36',
'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36',
'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'
]
#设置ip代理池
proxy_ips = [
'120.78.153.19:1234 ',
'221.6.215.202:9091 ',
'180.183.66.223:8080 ',
]
headers = {
#随机选择一个用户代理
random.choice(USER_AGENTS)
}
proxy_ip = {
# 随机选择一个ip池中的ip
random.choice(proxy_ips)
}
# get直接返回,不再等待界面加载完成
desired_capabilities = DesiredCapabilities.CHROME
desired_capabilities["pageLoadStrategy"] = "none"
# 设置谷歌驱动器的环境
options = webdriver.ChromeOptions()
# chrome_options = Options()
# options.add_argument('--headless') # 启用无头模式 即:设置不显示窗口
# 添加启动参数: chrome_options.add_argument()
# 可选:为了进一步模拟真实用户行为,可以添加一些其他的配置
options.add_argument('--disable-gpu') # 禁用GPU加速
options.add_argument('--no-sandbox') # 在Linux系统上使用时可能需要添加此选项
# 添加伪装请求头
options.add_argument(f'user-agent={headers}')
# 添加动态ip
# options.add_argument(f'--proxy-serve=http://{proxy_ip}')
#禁用自动化栏
options.add_experimental_option('excludeSwitches',['enable-automation'])
#屏蔽保存密码提示框
prefs={'credentials_enable_service':False,'profile.password_manager_enabled':False}
options.add_experimental_option('prefs',prefs)
#反爬虫特征处理
options.add_argument('--disable-blink-features=AutomationControlled')
# 设置chrome不加载图片,提高速度
# options.add_experimental_option("prefs", {"profile.managed_default_content_settings.images": 2})
# 创建一个谷歌驱动器
driver = webdriver.Chrome(options=options)
driver.maximize_window() #窗口最大化
标签:浏览器,like,Chrome,KHTML,配置,selenium,add,537.36,options
From: https://www.cnblogs.com/yin-jihu/p/18179144