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 20:48:08 +08:00 · 8361 次点击
    这是一个创建于 2380 天前的主题,其中的信息可能已经有所发展或是发生改变。

    用 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 20:59:24 +08:00 via iPhone
    记得有 headless 的 chrome 驱动的,一个 exe 程序,不需要设置别的
    icedx
        2
    icedx  
       2017-10-12 21:26:36 +08:00
    driver =webdriver.Chrome(chrome_options=chrome_options)
    qqpkat2
        3
    qqpkat2  
       2017-10-12 21:50:00 +08:00
    我用 J***r 根本不需要驱动什么的,直接运行,还能操作浏览器
    swirling
        5
    swirling  
       2017-10-12 22:23:09 +08:00
    2 楼正解 是你没用 options
    swirling
        6
    swirling  
       2017-10-12 22:24:36 +08:00
    self.driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)
    还应该有 chrome 的 binary path 和 driver 的 path 你都没加为啥也能运行。。。
    jugelizi
        7
    jugelizi  
       2017-10-12 22:36:13 +08:00
    @swirling 在系统环境变量里就不需要指定了
    yuxianghe
        8
    yuxianghe  
       2017-10-13 09:52:28 +08:00
    selenium 使用 chrome 的 headless 模式: https://www.yuxianghe.net/article/50
    yuxianghe
        9
    yuxianghe  
       2017-10-13 09:55:25 +08:00
    如果是 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 16:48:20 +08:00
    用 splinter 很方便就实现了。
    pip install splinter
    ```
    from splinter import Browser
    browser = Browser('chrome', headless=True)
    ```
    saximi
        11
    saximi  
    OP
       2017-10-13 23:36:52 +08:00
    @yuxianghe 谢谢,我先学习一下这篇文章
    saximi
        12
    saximi  
    OP
       2017-10-13 23:37:07 +08:00
    @choury 谢谢,可惜无法翻墙看不了
    saximi
        13
    saximi  
    OP
       2017-10-13 23:51:41 +08:00
    @WoodenRobot 感谢,我试试看,看起来很方便
    saximi
        14
    saximi  
    OP
       2017-10-16 21:54:16 +08:00
    @WoodenRobot
    browser = Browser('chrome', headless=True)
    请问上面语句返回的 browser 对象如何与 selenium 的 webdriver 结合起来使用,使得可以操作 selenium 的 webdriver 方法返回的对象呢?
    shawndev
        15
    shawndev  
       2017-10-17 13:02:11 +08:00
    你没发现你没用你的 chrome_options 吗,2 楼正解
    saximi
        16
    saximi  
    OP
       2017-10-17 19:54:50 +08:00
    @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 14:36:43 +08:00
    @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 19:31:00 +08:00
    @WoodenRobot 感谢!
    shawndev
        19
    shawndev  
       2017-10-19 10:37:53 +08:00
    @saximi 抱歉我的个人开发环境和工作开发环境均为 mac,mac 下无此问题。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3469 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 10:39 · PVG 18:39 · LAX 03:39 · JFK 06:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.