V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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
creatorYC
V2EX  ›  Python

Python 使用 requests 获取到 V2EX 某个话题页的内容与查看网页源代码的结果不一样,有部分 html 代码缺失,不知道是什么原因啊?

  •  
  •   creatorYC ·
    creatorYC · May 20, 2017 · 3469 views
    This topic created in 3269 days ago, the information mentioned may be changed or developed.

    例如 V2EX 的某个话题页的 url 是: https://www.v2ex.com/t/362535 ,使用 requests 获取内容的代码为:

    	resp = session.get(url, headers=headers)
        print resp.content
         
    

    我以为是动态网页,又使用了 phantomjs 和 selenium ,代码为:

    	driver = webdriver.PhantomJS("D:/phantomjs-2.1.1-windows/bin/phantomjs.exe")
    	driver.get(url)
    	print driver.page_source
         
    

    跟查看网页源代码的结果仍然不一样,部分 html 代码缺失了。 这是什么原因啊,真心求教。

    12 replies    2017-05-21 23:25:58 +08:00
    creatorYC
        2
    creatorYC  
    OP
       May 20, 2017
    @oh 我的目的不是这个,是为了模拟点击“收藏”,“感谢”之类的功能,可是官方没有这些 API
    katsusan
        3
    katsusan  
       May 20, 2017
    试了一下登录状态会影响到网页源代码的好像
    creatorYC
        4
    creatorYC  
    OP
       May 20, 2017
    @katsusan 意思是要先登录吗?我是模块测试的,所以没有登录,我去试试,谢谢!
    creatorYC
        5
    creatorYC  
    OP
       May 20, 2017
    问题已经解决了,需要登录。谢谢大家
    katsusan
        6
    katsusan  
       May 20, 2017 via iPhone
    @creatorYC 不客气,其实我是小白中的白中白←_←
    dd99iii
        7
    dd99iii  
       May 21, 2017 via iPhone
    虽然 requests 也没问题,不过既然 ve2x 提供了 API 就用吧
    creatorYC
        8
    creatorYC  
    OP
       May 21, 2017
    @dd99iii 这个需求没有 API 啊,有 API 的都用了 API 哦
    creatorYC
        10
    creatorYC  
    OP
       May 21, 2017
    @dd99iii 比如收藏某个话题,关注某个用户
    dd99iii
        11
    dd99iii  
       May 21, 2017
    @creatorYC 那的确是
    creatorYC
        12
    creatorYC  
    OP
       May 21, 2017
    @dd99iii 嗯,所以自己爬到了,嘿嘿
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2340 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 01:38 · PVG 09:38 · LAX 18:38 · JFK 21:38
    ♥ Do have faith in what you're doing.