python+selenium+Chrome options参数的使用
ChromeOptions常用的行为一般有以下几种:
- 禁止图片和视频的加载:提升网页加载速度。
- 添加代理:用于翻墙访问某些页面,或者应对IP访问频率限制的反爬技术。
- 使用移动头:访问移动端的站点,一般这种站点的反爬技术比较薄弱。
- 添加扩展:像正常使用浏览器一样的功能。
- 设置编码:应对中文站,防止乱码。
- 阻止JavaScript执行
- ...
ChromeOptions是一个配置chrome启动时属性的类,通过这个参数我们可以为Chrome添加如下参数:
- 设置chrome二进制文件位置(binary_location)
- 添加启动参数(add_argument)
- 添加扩展应用(add_extension,add_encoded_extension)
- 添加实验性质的设置参数(add_experimental_option)
- 设置调试器地址(debugger_address)
针对编码格式的操作
#设置默认编码为utf-8 fromseleniumimportwebdriver options=webdriver.ChromeOptions() options.add_argument('lang=zh_CN.UTF-8') driver=webdriver.Chrome(chrome_options=options)
针对UA请求头的操作
#设置请求头为huaweiMeta10Pro fromseleniumimportwebdriver options=webdriver.ChromeOptions() options.add_argument('User-Agent=Mozilla/5.0(Linux;U;Android8.1.0;zh-cn;BLA-AL00Build/HUAWEIBLA-AL00)AppleWebKit/537.36(KHTML,likeGecko)Version/4.0Chrome/57.0.2987.132MQQBrowser/8.9MobileSafari/537.36') options.add_argument('--headless')#浏览器不提供可视化页面 driver=webdriver.Chrome(chrome_options=options)
http://www.fynas.com/ua
针对禁止加载图片的操作
#设置浏览器禁止加载图片 fromseleniumimportwebdriver options=webdriver.ChromeOptions() prefs={"profile.managed_default_content_settings.images":2} options.add_experimental_option("prefs",prefs) driver=webdriver.Chrome(chrome_options=options)
针对IP代理的操作
特别需要注意,在选择代理时,尽量选择静态IP,才能提升爬取的稳定性。如果使用动态匿名IP,每个IP的存活时间是很短的。
#设置无账号密码的代理 chromeOptions=webdriver.ChromeOptions() chromeOptions.add_argument('--proxy-server=http://ip:port') driver=webdriver.Chrome(chrome_options=chromeOptions)
#设置有账号密码的代理 proxyauth_plugin_path=create_proxyauth_extension( proxy_host='host', proxy_port='port', proxy_username="username", proxy_password="password" ) options.add_extension(proxyauth_plugin_path)
查看IP地址的链接:http://httpbin.org/ip
针对添加插件的操作
#添加xpathhelper应用 fromseleniumimportwebdriver chrome_options=webdriver.ChromeOptions() #设置好应用扩展 extension_path='yourfile_path' chrome_options.add_extension(extension_path)
针对登录时关闭弹出的密码保存提示框
fromseleniumimportwebdriver fromselenium.webdriver.common.byimportBy options=webdriver.ChromeOptions() prefs={} #设置这两个参数就可以避免密码提示框的弹出 prefs[“credentials_enable_service”]=False prefs[“profile.password_manager_enabled”]=False options.add_experimental_option(“prefs”,prefs) browser=webdriver.Chrome(chrome_options=options) browser.get('https://www.baidu.com/') browser.quit()
其它配置
options.add_argument('--disable-infobars')#禁止策略化 options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错 options.add_argument('window-size=1920x3000')#指定浏览器分辨率 options.add_argument('--disable-gpu')#谷歌文档提到需要加上这个属性来规避bug options.add_argument('--incognito')#隐身模式(无痕模式) options.add_argument('--disable-javascript')#禁用javascript options.add_argument('--start-maximized')#最大化运行(全屏窗口),不设置,取元素会报错 options.add_argument('--disable-infobars')#禁用浏览器正在被自动化程序控制的提示 options.add_argument('--hide-scrollbars')#隐藏滚动条,应对一些特殊页面 options.add_argument('blink-settings=imagesEnabled=false')#不加载图片,提升速度 options.add_argument('--headless')#浏览器不提供可视化页面.linux下如果系统不支持可视化不加这条会启动失败 options.binary_location=r"C:\ProgramFiles(x86)\Google\Chrome\Application\chrome.exe"#手动指定使用的浏览器位置
更多插件操作请参考:https://www.nhooo.com/article/182967.htm
到此这篇关于python+selenium+Chromeoptions参数的使用的文章就介绍到这了,更多相关seleniumChromeoptions参数内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!