【问题标题】:Why is python's urllib2.urlopen giving me a 403 error? [duplicate]为什么 python 的 urllib2.urlopen 给我一个 403 错误? [复制]
【发布时间】:2013-01-23 01:51:34
【问题描述】:

可能重复:
Python’s urllib2 doesn’t work on some sites

好的,我只想用python访问这个网址:http://www.gocomics.com/wizardofid/2013/01/22

但是,每当我调用 urllib2.urlopen('http://www.gocomics.com/wizardofid/2013/01/22').read() 时,都会出现 403 错误。使用 urllib,我所能做的就是阅读错误页面,但 urllib2 会引发错误。当我在 Chrome 中查看该页面时,它不会给我带来任何问题。为什么会这样,我该如何解决?谢谢!

【问题讨论】:

标签: python urllib2 urllib http-status-code-403 python-2.5


【解决方案1】:

此特定网站需要“类似浏览器”的User-Agent 标头,否则将拒绝访问。

尝试添加标题,例如(例如)这样:

import urllib2

opener = urllib2.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib2.install_opener(opener)
print urllib2.urlopen('http://gocomics.com/wizardofid/2013/01/22').read()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-14
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2018-01-26
    相关资源
    最近更新 更多