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

求助,为什么 Requests 不能获取到知乎的页面?

  •  
  •   nlimpid ·
    nlimpid · 2016-07-09 13:38:37 +08:00 · 6373 次点击
    这是一个创建于 2849 天前的主题,其中的信息可能已经有所发展或是发生改变。
    >>> requests.get("http://github.com")
    <Response [200]>
    >>> requests.get("https://github.com")
    <Response [200]>
    >>> requests.get("https://www.baidu.com")
    <Response [200]>
    >>> requests.get("http://zhihu.com")
    <Response [500]>
    >>> requests.get("https://zhihu.com")
    <Response [500]>

    但是用 urlopen 可以,不知道为什么,求解。
    8 条回复    2016-07-10 19:25:27 +08:00
    hebwjb
        1
    hebwjb  
       2016-07-09 13:45:17 +08:00   ❤️ 1
    header = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"}

    requests.get('http://zhihu.com',headers=header)
    <Response [200]>
    coolloves
        2
    coolloves  
       2016-07-09 13:48:19 +08:00
    加个 headers 就可以了把
    nlimpid
        3
    nlimpid  
    OP
       2016-07-09 13:48:50 +08:00
    @hebwjb 感谢,但是为什么呢?
    zwh8800
        4
    zwh8800  
       2016-07-09 14:10:56 +08:00
    @nlimpid 一般网站都会检查 ua
    GreatMartial
        5
    GreatMartial  
       2016-07-09 16:00:19 +08:00
    @nlimpid 有的网站要检查你的请求环境,你不模拟,就认定你是机器
    CosimoZi
        6
    CosimoZi  
       2016-07-09 18:13:33 +08:00
    知乎现在防爬虫越来越严格了……之前我爬还不需要 headers 就能上。
    tobacco
        7
    tobacco  
       2016-07-09 20:39:52 +08:00
    有现成的知乎爬虫: https://github.com/egrcc/zhihu-python
    nlimpid
        8
    nlimpid  
    OP
       2016-07-10 19:25:27 +08:00
    @tobacco 感谢,但我不是想做爬虫。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1848 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 16:18 · PVG 00:18 · LAX 09:18 · JFK 12:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.