V2EX › Python
请问 selenium 包加 Chrome 开发爬虫时,如何使用 Chrome 的 headless 模式?
By saximi at 2017 年 10 月 12 日 · 9692 次点击用 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
|
1
kylinking 2017 年 10 月 12 日 via iPhone
记得有 headless 的 chrome 驱动的,一个 exe 程序,不需要设置别的
|
|
2
icedx 2017 年 10 月 12 日
driver =webdriver.Chrome(chrome_options=chrome_options)
|
|
3
qqpkat2 2017 年 10 月 12 日
我用 J***r 根本不需要驱动什么的,直接运行,还能操作浏览器
|
|
4
choury 2017 年 10 月 12 日
|
|
5
swirling 2017 年 10 月 12 日
2 楼正解 是你没用 options
|
|
6
swirling 2017 年 10 月 12 日
self.driver = webdriver.Chrome(executable_path=chrome_driver, chrome_options=chrome_options)
还应该有 chrome 的 binary path 和 driver 的 path 你都没加为啥也能运行。。。 |
|
8
yuxianghe 2017 年 10 月 13 日
selenium 使用 chrome 的 headless 模式: https://www.yuxianghe.net/article/50
|
|
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. |
|
10
WoodenRobot 2017 年 10 月 13 日
用 splinter 很方便就实现了。
pip install splinter ``` from splinter import Browser browser = Browser('chrome', headless=True) ``` |
|
13
saximi OP @WoodenRobot 感谢,我试试看,看起来很方便
|
|
14
saximi OP @WoodenRobot
browser = Browser('chrome', headless=True) 请问上面语句返回的 browser 对象如何与 selenium 的 webdriver 结合起来使用,使得可以操作 selenium 的 webdriver 方法返回的对象呢? |
|
15
shawndev 2017 年 10 月 17 日
你没发现你没用你的 chrome_options 吗,2 楼正解
|
|
16
saximi OP @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) |
|
17
WoodenRobot 2017 年 10 月 18 日
@saximi 详细内容可以查看:
Splinter 官方文档: https://splinter.readthedocs.io/en/latest/ Splinter 中文文档: http://splinter-docs-zh-cn.readthedocs.io/zh/latest/ |
|
18
saximi OP @WoodenRobot 感谢!
|
推荐学习书目
› 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