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

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

  •  
  •   creatorYC ·
    creatorYC · 2017-05-20 21:01:23 +08:00 · 3001 次点击
    这是一个创建于 2531 天前的主题,其中的信息可能已经有所发展或是发生改变。

    例如 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 条回复    2017-05-21 23:25:58 +08:00
    oh
        1
    oh  
       2017-05-20 21:48:38 +08:00
    creatorYC
        2
    creatorYC  
    OP
       2017-05-20 21:55:50 +08:00
    @oh 我的目的不是这个,是为了模拟点击“收藏”,“感谢”之类的功能,可是官方没有这些 API
    katsusan
        3
    katsusan  
       2017-05-20 22:03:46 +08:00
    试了一下登录状态会影响到网页源代码的好像
    creatorYC
        4
    creatorYC  
    OP
       2017-05-20 22:07:13 +08:00
    @katsusan 意思是要先登录吗?我是模块测试的,所以没有登录,我去试试,谢谢!
    creatorYC
        5
    creatorYC  
    OP
       2017-05-20 22:25:25 +08:00
    问题已经解决了,需要登录。谢谢大家
    katsusan
        6
    katsusan  
       2017-05-20 23:12:08 +08:00 via iPhone
    @creatorYC 不客气,其实我是小白中的白中白←_←
    dd99iii
        7
    dd99iii  
       2017-05-21 15:45:02 +08:00 via iPhone
    虽然 requests 也没问题,不过既然 ve2x 提供了 API 就用吧
    creatorYC
        8
    creatorYC  
    OP
       2017-05-21 16:16:21 +08:00
    @dd99iii 这个需求没有 API 啊,有 API 的都用了 API 哦
    dd99iii
        9
    dd99iii  
       2017-05-21 23:04:59 +08:00
    creatorYC
        10
    creatorYC  
    OP
       2017-05-21 23:12:14 +08:00
    @dd99iii 比如收藏某个话题,关注某个用户
    dd99iii
        11
    dd99iii  
       2017-05-21 23:16:11 +08:00
    @creatorYC 那的确是
    creatorYC
        12
    creatorYC  
    OP
       2017-05-21 23:25:58 +08:00
    @dd99iii 嗯,所以自己爬到了,嘿嘿
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3963 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:20 · PVG 18:20 · LAX 03:20 · JFK 06:20
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.