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

python2+headless chrome+selenium,如何解决出现 js-disabled 的提示

  •  
  •   akmonde · 2018-04-09 00:07:24 +08:00 · 2621 次点击
    这是一个创建于 2201 天前的主题,其中的信息可能已经有所发展或是发生改变。

    RT,鄙人在网上直接扒下来无界面浏览器的样例,需要爬一个带 dom 型内容的网页:

    #coding=utf-8
    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.common.action_chains import ActionChains
    from selenium.webdriver.common.keys import Keys
    
    chrome_options = webdriver.ChromeOptions()
    # 使用 headless 无界面浏览器模式
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--disable-gpu')
    
    # 启动浏览器,获取网页源代码
    browser = webdriver.Chrome(chrome_options=chrome_options)
    mainUrl = "https://xxx.com"
    browser.get(mainUrl)
    #print(f"browser text = {browser.page_source}")
    print browser.page_source
    browser.quit()
    

    按理说应该问题不大,原案例网址是可以爬出东西的。 结果我填入我需要爬的网页,除了一些必要的该网页的 title 和 meta 信息,剩下的就是下面这样:

      <noscript>
      &lt;div class="js-disabled"&gt;
        It looks like your JavaScript is disabled. To use XXX, enable JavaScript in your browser and refresh this page.
      &lt;/div&gt;
    </noscript>
    

    敢问各位大佬,这是网站自己做了防爬措施,还是我需要启用其他的 option ? 最关键是,我试着自己通过 chrome_options.add_argument,又加了些 headless chrome 的 option,参考资料如下:

    http://www.assertselenium.com/java/list-of-chrome-driver-command-line-arguments/
    

    但结果并没有什么卵用,还是会出现 noscript 的提示,求解惑应该怎样解决这个问题。

    另外,样站是客户要去测的,这里不太方便直接发出网址,望各位大佬见谅!

    8 条回复    2018-06-17 22:26:56 +08:00
    akmonde
        1
    akmonde  
    OP
       2018-04-09 08:42:12 +08:00
    有大佬能帮小弟解答一二么...我看了几条消息全都是收藏...
    hareandlion
        2
    hareandlion  
       2018-04-09 08:55:31 +08:00 via iPhone
    selenium 用得不多,基本都是爬静态资源,请问什么情况下需要用到网页的 js ?谢谢
    gimp
        3
    gimp  
       2018-04-09 09:04:20 +08:00
    默认应该是可以解析 js 的,进一步确定问题所在可以尝试以下

    1,换用 Firefox 跑一下看看能不能解析,如果可以,说明问题还在 chrome headless 设置问题
    2,找个 JS 动态渲染的网页,比如 http://data.eastmoney.com/report/ 看看能不能渲染
    zbl430
        4
    zbl430  
       2018-04-09 11:47:24 +08:00
    不发网址。。。不好搞啊
    akmonde
        5
    akmonde  
    OP
       2018-04-09 12:00:43 +08:00
    @zbl430 客户的保密项目,我也很绝望啊..刚找私信也找不到。
    akmonde
        6
    akmonde  
    OP
       2018-04-09 12:31:37 +08:00
    @gimp eastmoney 的站可以渲染的,chrome 和 firefox 都是那个 disable 的提示,很绝望..
    akmonde
        7
    akmonde  
    OP
       2018-04-09 12:32:29 +08:00
    @hareandlion dom 形成的网页资源,直接 requests,urllib*返回的内容里读不到的,需要模拟浏览器。
    wwek
        8
    wwek  
       2018-06-17 22:26:56 +08:00
    界面浏览器访问看有哪些 ajax 请求
    然后无界面的再来一遍,抓包对比
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3213 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 13:05 · PVG 21:05 · LAX 06:05 · JFK 09:05
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.