selenium谷歌无头浏览器实例

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值