urllib2 错误 403:禁止 [英] urllib2 Error 403: Forbidden

查看:35
本文介绍了urllib2 错误 403:禁止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这个网站上发帖了,并得到了非常有用的指导,我带着另一个问题回来了.

I have posted to this site and received really helpful guidance, i return with another question.

我哪里出错了,我很确定这是从各个站点访问信息所必需的.在这种情况下,芝商所集团.

Where have i gone wrong here, I was prettty sure this is what is required to access information from various sites. In this case, the CME Group.

   import urllib2

url = "http://www.cmegroup.com/trading/energy/natural-gas/natural-gas.html"
request= urllib2.Request(url)
handle = urllib2.urlopen(request)
content = handle.read()
splitted_page = content.split("<span class=\"cmeSubHeading\">", 1);
splitted_page = splitted_page[1].split("</span>", 1)
print splitted_page[0]

读取错误,

HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 403: Forbidden

非常感谢您.

推荐答案

实际上问题是他们屏蔽了所有没有用户代理的人

Actually the problem is that they block everyone who doesn't have a user-agent

import urllib2

request = urllib2.Request("http://www.cmegroup.com/trading/energy/natural-gas/natural-gas.html", None, {'User-Agent': 'Mozilla/5.0'})
content = urllib2.urlopen(request).read()
splitted_page = content.split("<span class=\"cmeSubHeading\">", 1);
splitted_page = splitted_page[1].split("</span>", 1)
print splitted_page[0]

这篇关于urllib2 错误 403:禁止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆