请问 selenium 包加 Chrome 开发爬虫时,如何使用 Chrome 的 headless 模式? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
saximi
V2EX    Python

请问 selenium 包加 Chrome 开发爬虫时,如何使用 Chrome 的 headless 模式?

  •  
  •   saximi 2017 年 10 月 12 日 9058 次点击
    这是一个创建于 3018 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 selenium+Chrome 开发爬虫时,想使用 Chrome 的 headless 模式,用了以下的语句,结果发现无效,浏览器依然还会出现,请问正确的写法应该是什么呢?感谢!

    from selenium.webdriver.chrome.options import Options chrome_optiOns= Options() chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') driver =webdriver.Chrome() 
    19 条回复    2017-10-19 10:37:53 +08:00
    kylinking
        1
    kylinking  
       2017 年 10 月 12 日 via iPhone
    记得有 headless 的 chrome 驱动的,一个 exe 程序,不需要设置别的
    icedx
        2
    icedx  
       2017 年 10 月 12 日
    driver =webdriver.Chrome(chrome_optiOns=chrome_options)
    qqpkat2
        3
    qqpkat2  
       2017 年 10 月 12 日
    我用 J***r 根本不需要驱动什么的,直接运行,还能操作浏览器
    swirling
        5
    swirling  
       2017 年 10 月 12 日
    2 楼正解 是你没用 options
    swirling
        6
    swirling  
       2017 年 10 月 12 日
    self.driver = webdriver.Chrome(executable_path=chrome_driver, chrome_optiOns=chrome_options)
    还应该有 chrome 的 binary path 和 driver 的 path 你都没加为啥也能运行。。。
    jugelizi
        7
    jugelizi  
       2017 年 10 月 12 日
    @swirling 在系统环境变量里就不需要指定了
    yuxianghe
        8
    yuxianghe  
       2017 年 10 月 13 日
    selenium 使用 chrome 的 headless 模式: https://www.yuxianghe.net/article/50
    yuxianghe
        9
    yuxianghe  
       2017 年 10 月 13 日
    如果是 windows 环境,确保你的 chrome 浏览器版本是 60+,不然也不行。
    官方回答:
    Caution: Headless mode is available on Mac and Linux in Chrome 59. Windows support is coming in Chrome 60. To check what version of Chrome you have, open chrome://version.
    WoodenRobot
        10
    WoodenRobot  
       2017 年 10 月 13 日
    用 splinter 很方便就实现了。
    pip install splinter
    ```
    from splinter import Browser
    browser = Browser('chrome', headless=True)
    ```
    saximi
        11
    saximi  
    OP
       2017 年 10 月 13 日
    @yuxianghe 谢谢,我先学习一下这篇文章
    saximi
        12
    saximi  
    OP
       2017 年 10 月 13 日
    @choury 谢谢,可惜无法翻墙看不了
    saximi
        13
    saximi  
    OP
       2017 年 10 月 13 日
    @WoodenRobot 感谢,我试试看,看起来很方便
    saximi
        14
    saximi  
    OP
       2017 年 10 月 16 日
    @WoodenRobot
    browser = Browser('chrome', headless=True)
    请问上面语句返回的 browser 对象如何与 selenium 的 webdriver 结合起来使用,使得可以操作 selenium 的 webdriver 方法返回的对象呢?
    shawndev
        15
    shawndev  
       2017 年 10 月 17 日
    你没发现你没用你的 chrome_options 吗,2 楼正解
    saximi
        16
    saximi  
    OP
       2017 年 10 月 17 日
    @shawndev 写了下面的代码,运行时是不会出现 chrome 浏览器,但是总是在一开始还是会弹出 chromedriver.exe 的命令窗口,请问这个弹窗要如何才能取消呢?感谢!
    chrome_optiOns= Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    driver =webdriver.Chrome(chrome_optiOns=chrome_options)
    WoodenRobot
        17
    WoodenRobot  
       2017 年 10 月 18 日
    @saximi 详细内容可以查看:
    Splinter 官方文档: https://splinter.readthedocs.io/en/latest/
    Splinter 中文文档: http://splinter-docs-zh-cn.readthedocs.io/zh/latest/
    saximi
        18
    saximi  
    OP
       2017 年 10 月 18 日
    @WoodenRobot 感谢!
    shawndev
        19
    shawndev  
       2017 年 10 月 19 日
    @saximi 抱歉我的个人开发环境和工作开发环境均为 mac,mac 下无此问题。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2697 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 24ms UTC 12:27 PVG 20:27 LAX 04:27 JFK 07:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86