试图从Python访问网页时的状态码403


403 status code while trying to access webpage from Python

我已经尝试过使用JSON,但无法真正读取此页面。

这是我的python代码。我已经在其他网站上尝试过了,但是在这个网站上它返回一个403。

import urllib2
req = urllib2.Request('http://www.taringa.net/envivo/ajax.php')
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

更好地使用请求。我尝试了你的脚本,得到了403的状态。这意味着访问它是关闭的,无论出于什么原因,我不知道。

您必须添加'User-Agent'头才能使此工作。

Urllib代码:

req = urllib2.Request('http://www.taringa.net/envivo/ajax.php')
req.add_header('User-Agent', 'Mozilla')
resp = urllib2.urlopen(req)
print resp.code  # Gives 200.
print resp.read()  # Gives the HTML of the page.

我建议你使用请求,主要是因为它使这类事情非常容易。

请求代码:

h = {'User-Agent':'Mozilla'}
requests.get('http://www.taringa.net/envivo/ajax.php', headers=h)