使用urllib2获取网络资源,出现http 402错误
Using urllib2 to fetch internet resources, get http 402 error
我尝试使用 urllib2 从字幕网站获取 zip 文件。
示例网站是 http://sub.makedie.me and I tried to download this file http://sub.makedie.me/download/601943/Game%20of%20Thrones%20-%2005x08%20-%20Hardhome.KILLERS.English.HI.C.orig.Addic7ed.com.zip
我在脚本中进行了测试并打印了 url。 url 很好。我在网络浏览器中复制并粘贴,我可以成功下载它。
起初,脚本看起来像这样:
try:
f = urllib2.urlopen(example_url)
f.read()
something...
except URLError, e:
print e.code
但是我得到了 403 错误代码。经过搜索,我尝试将 header 更改为 {'User-Agent': 'Mozilla/5.0'}。代码更改为:
try:
req = urllib2.Request(example_url,headers={'User-Agent': 'Mozilla/5.0'})
f = urllib2.urlopen(req)
something...
except URLError, e:
print e.code
然后我得到了 402 错误。我想知道这是因为网站设置还是因为我的代码错误?
402 表示请求目前无效。
保留以备将来使用。
来自 http://en.wikipedia.org/wiki/List_of_HTTP_status_codes :
402 需要付款
保留供将来使用。最初的意图是此代码可能用作某种形式的数字现金或小额支付方案的一部分,但这并没有发生,并且通常不使用此代码。如果特定 IP 地址发出过多请求,YouTube 会使用此状态,并要求此人输入验证码。
因此可能涉及导致问题的验证码。
检查站点的 Robots.txt 文件:www.domain_name.com/robots.txt
我会尝试:
urllib.urlretrieve(url, outname)
因为您正在尝试下载文件而不是打开文件。
我尝试使用 urllib2 从字幕网站获取 zip 文件。
示例网站是 http://sub.makedie.me and I tried to download this file http://sub.makedie.me/download/601943/Game%20of%20Thrones%20-%2005x08%20-%20Hardhome.KILLERS.English.HI.C.orig.Addic7ed.com.zip
我在脚本中进行了测试并打印了 url。 url 很好。我在网络浏览器中复制并粘贴,我可以成功下载它。
起初,脚本看起来像这样:
try:
f = urllib2.urlopen(example_url)
f.read()
something...
except URLError, e:
print e.code
但是我得到了 403 错误代码。经过搜索,我尝试将 header 更改为 {'User-Agent': 'Mozilla/5.0'}。代码更改为:
try:
req = urllib2.Request(example_url,headers={'User-Agent': 'Mozilla/5.0'})
f = urllib2.urlopen(req)
something...
except URLError, e:
print e.code
然后我得到了 402 错误。我想知道这是因为网站设置还是因为我的代码错误?
402 表示请求目前无效。
保留以备将来使用。
来自 http://en.wikipedia.org/wiki/List_of_HTTP_status_codes :
402 需要付款
保留供将来使用。最初的意图是此代码可能用作某种形式的数字现金或小额支付方案的一部分,但这并没有发生,并且通常不使用此代码。如果特定 IP 地址发出过多请求,YouTube 会使用此状态,并要求此人输入验证码。
因此可能涉及导致问题的验证码。
检查站点的 Robots.txt 文件:www.domain_name.com/robots.txt
我会尝试:
urllib.urlretrieve(url, outname)
因为您正在尝试下载文件而不是打开文件。