def get_chrome_driver_instance():
# 谷歌无头模式
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--start-maximized')
options.add_argument('--lang=zh_CN.UTF-8')
options.add_argument('--no-sandbox')
options.add_argument('--disable-gpu')
options.add_argument('disable-infobars')
options.add_argument('--ignore-certificate-errors')
options.add_argument('--disable-extensions')
options.add_argument('--disable-desktop-notifications')
options.add_argument('--no-default-browser-check')
options.add_argument('--disable-extensions')
options.add_argument('--disable-default-apps')
options.add_argument('disable-popup-blocking')
options.add_argument('--disable-sync')
options.add_argument('--disable-translate')
options.add_argument('--disable-zero-browsers-open-for-tests')
options.add_argument('--allow-file-access')
options.add_argument('--allow-file-access-from-files=about:blank')
options.add_argument('–-no-first-run')
options.add_argument('--enable-quic')
options.add_argument('--google-base-url=127.0.0.1')
# 更换头部
user_agent = (
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.57 Safari/537.36"
)
options.add_argument('user-agent=%s' % user_agent)
# 设置图片不加载
prefs = {
'profile.default_content_setting_values': {
'images': 2
}
}
options.add_experimental_option('prefs', prefs)
# 或者 使用下面的设置, 提升速度
options.add_argument('blink-settings=imagesEnabled=false')
chrome_driver_instance = webdriver.Chrome(chrome_options=options)
chrome_driver_instance.set_page_load_timeout(10)
chrome_driver_instance.set_script_timeout(10)
return chrome_driver_instance
selenium谷歌无头浏览器实例
最新推荐文章于 2024-07-16 11:04:39 发布