python + selenium + Chrome options parameters

python + selenium + Chrome options parameters

Chrome Options common behavior generally have the following:

禁止图片和视频的加载:提升网页加载速度。
添加代理:用于翻墙访问某些页面,或者应对IP访问频率限制的反爬技术。
使用移动头:访问移动端的站点,一般这种站点的反爬技术比较薄弱。
添加扩展:像正常使用浏览器一样的功能。
设置编码:应对中文站,防止乱码。
阻止JavaScript执行
...

Chrome Options is a configuration chrome start attribute class, we can add the following parameters for the Chrome by this parameter:

设置 chrome 二进制文件位置 (binary_location)
添加启动参数 (add_argument)
添加扩展应用 (add_extension, add_encoded_extension)
添加实验性质的设置参数 (add_experimental_option)
设置调试器地址 (debugger_address)

For the operation of the encoding format

# 设置默认编码为 utf-8
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('lang=zh_CN.UTF-8')
driver = webdriver.Chrome(chrome_options = options)

UA requests for the operating head

# 设置请求头为huaweiMeta10 Pro
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('User-Agent=Mozilla/5.0 (Linux; U; Android 8.1.0; zh-cn; BLA-AL00 Build/HUAWEIBLA-AL00) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/57.0.2987.132 MQQBrowser/8.9 Mobile Safari/537.36')
options.add_argument('--headless')  # 浏览器不提供可视化页面
driver = webdriver.Chrome(chrome_options = options)
http://www.fynas.com/ua

For the operation prohibited Load picture

# 设置浏览器禁止加载图片
from selenium import webdriver
options = webdriver.ChromeOptions()
prefs = {"profile.managed_default_content_settings.images": 2}
options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome(chrome_options = options)

IP Agent for the operation

特别需要注意,在选择代理时,尽量选择静态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)

Check the IP address of the link: http://httpbin.org/ip

Add plug-ins for operation

# 添加xpath helper应用

from selenium import webdriver
chrome_options = webdriver.ChromeOptions()

# 设置好应用扩展
extension_path = 'your file_path'
chrome_options.add_extension(extension_path)

Close pop-up for when the password prompt box saved

from selenium import webdriver 
from selenium.webdriver.common.by import By
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:\Program Files (x86)\Google\Chrome\Application\chrome.exe"  # 手动指定使用的浏览器位置

More plug operations, refer to: https://blog.csdn.net/liaojianqiu0115/article/details/78353267

Guess you like

Origin www.cnblogs.com/guapitomjoy/p/12150416.html