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
kukuwhu
V2EX  ›  Python

用 selenium 时,除了 phantomjs 还有哪些浏览器支持 headless mode(无桌面环境的命令行)运行?

  •  
  •   kukuwhu · 2016-10-18 15:34:57 +08:00 · 3321 次点击
    这是一个创建于 2753 天前的主题,其中的信息可能已经有所发展或是发生改变。

    在做爬虫的时候,经常碰到 requests 搞不定的时候上 webdriver ,但基于 selenium 的 chrome 必须在桌面环境运行(会弹出浏览器窗口),而用支持命令行启动的 phanjstom 请求的时候很多网页都有反爬(不知道怎么识别的),有没有其他的浏览器支持 Linux 的无窗口环境运行? firefox 之类的支持吗?谢谢

    11 条回复    2016-10-18 17:56:07 +08:00
    jetyang
        1
    jetyang  
       2016-10-18 15:55:34 +08:00   ❤️ 1
    firefox 、 chrome 都可以啊,配合 Xvfb 使用。如果用 chrome 要单独安装 chromedriver , firefox 不用额外装东西,网上教程有一些,挺容易配置的
    cheetah
        2
    cheetah  
       2016-10-18 16:02:49 +08:00
    ls 说的对,可以搜下 webdriver
    至于 phanjstom 怎么被识别的,只能是 ua 了吧
    Python 的话还可以用 ghost.py
    kukuwhu
        3
    kukuwhu  
    OP
       2016-10-18 16:03:09 +08:00
    @jetyang 好的,非常感谢,另外再请教下,理论上用 phantomjs 通过配置 desired_capabilities 可完全模拟 chrome,但为什么我用 chrome 可打开页面,用 phantomjs 返回的是明显做了反爬的空 html 页面?除了配置 desired_capabilities 的 useragent 为 chrome 还有其他地方有差异吗?
    kukuwhu
        4
    kukuwhu  
    OP
       2016-10-18 16:04:36 +08:00
    @cheetah 我是用的 chromedriver ,必须要在视窗界面下运行才不报错,应该是 jetyang 说的,需要配置一下 xvfb
    cheetah
        5
    cheetah  
       2016-10-18 16:05:52 +08:00
    @kukuwhu 是的
    jetyang
        6
    jetyang  
       2016-10-18 16:42:46 +08:00
    @kukuwhu 我没用过 phantomjs ,一直用的 webdriver 。别人防抓取的策略可能比较高级吧,比如限制单 IP 访问频次、 cookie 保持等等,爬不到正确内容要分析具体原因
    relic
        7
    relic  
       2016-10-18 16:53:37 +08:00
    @jetyang 请教一下,下载了 chromedriver 之后还用安装 chrome 吗?如果用怎么在命令行下安装 chrome 呢?
    jetyang
        8
    jetyang  
       2016-10-18 17:44:06 +08:00
    @relic 我都装了,环境是 centos ,必须 centos 7 以上才能装 chrome , yum install chrome , chromedriver 我记得是个可执行文件,下载解压就可以用了。好像还有一个坑: chrome 不能以 root 用户启动,记不清了,你试试看
    gimp
        9
    gimp  
       2016-10-18 17:51:36 +08:00
    安装 chrome 可以用这个全自动脚本,要求 Centos6 以上版本

    https://chrome.richardlloyd.org.uk/
    gimp
        10
    gimp  
       2016-10-18 17:55:10 +08:00
    之前折腾的时候整理点 Xvfb 和 selenium

    https://blog.ishell.me/a/selenium-with-xvfb.html
    gimp
        11
    gimp  
       2016-10-18 17:56:07 +08:00
    手抖... 还有个 https://blog.ishell.me/a/xvfb-screenshot-capture.html 稍微填了下上边的坑
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2475 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:10 · PVG 22:10 · LAX 07:10 · JFK 10:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.