Python IOError:(';http错误';,403,';禁止';,<;httplib.HTTPMessage实例位于0x7f98ec3d92d8>;)
我想下载这样的文件:Python IOError:(';http错误';,403,';禁止';,<;httplib.HTTPMessage实例位于0x7f98ec3d92d8>;),python,python-2.7,urllib,Python,Python 2.7,Urllib,我想下载这样的文件: import urllib link = 'http://ir.30nama.download/movies/t/The_Huntsman_Winters_War_2016_EXTENDED_Dubbed_1080p_BrRip_30nama_30NAMA.mkv?md5=E38HpAmjkzwU7Fpag-YvtA&expires=1529934194&refresh=4918368251152863819423374231251501' filename
import urllib
link = 'http://ir.30nama.download/movies/t/The_Huntsman_Winters_War_2016_EXTENDED_Dubbed_1080p_BrRip_30nama_30NAMA.mkv?md5=E38HpAmjkzwU7Fpag-YvtA&expires=1529934194&refresh=4918368251152863819423374231251501'
filename = link[link.rfind('/') + 1:].split('?')[0]
response = urllib.URLopener()
response.addheader('User-Agent',
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11')
response.addheader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
response.addheader('Accept-Charset', 'ISO-8859-1,utf-8;q=0.7,*;q=0.3')
response.addheader('Accept-Encoding', 'none')
response.addheader('Accept-Language', 'en-US,en;q=0.8')
response.addheader('Connection', 'keep-alive')
response.addheader('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
response.retrieve(link, 'test.mkv')
我添加了与此完全相同的标题,
但结果是:
Traceback (most recent call last):
File "downloader.py", line 29, in <module>
response.retrieve(item['src_link'], consts.pdp_root + filename)
File "/usr/lib/python2.7/urllib.py", line 245, in retrieve
fp = self.open(url, data)
File "/usr/lib/python2.7/urllib.py", line 213, in open
return getattr(self, name)(url)
File "/usr/lib/python2.7/urllib.py", line 364, in open_http
return self.http_error(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 381, in http_error
return self.http_error_default(url, fp, errcode, errmsg, headers)
File "/usr/lib/python2.7/urllib.py", line 386, in http_error_default
raise IOError, ('http error', errcode, errmsg, headers)
IOError: ('http error', 403, 'Forbidden', <httplib.HTTPMessage instance at 0x7f98ec3d92d8>)
回溯(最近一次呼叫最后一次):
文件“downloader.py”,第29行,在
response.retrieve(项['src_link'],consts.pdp_root+文件名)
文件“/usr/lib/python2.7/urllib.py”,第245行,在检索中
fp=self.open(url、数据)
文件“/usr/lib/python2.7/urllib.py”,第213行,打开
返回getattr(self,name)(url)
open_http中的文件“/usr/lib/python2.7/urllib.py”,第364行
返回self.http_错误(url、fp、errcode、errmsg、头)
文件“/usr/lib/python2.7/urllib.py”,第381行,http_错误
返回self.http\u error\u默认值(url、fp、errcode、errmsg、头)
文件“/usr/lib/python2.7/urllib.py”,第386行,默认为http\u error\u
raise IOError,('http error',errcode,errmsg,headers)
IOError:('http错误',403'禁止',)
我该怎么办?因此,30nama网站会阻止非浏览器代理。
您可以尝试流式请求
import requests
import shutil
link = 'http://ir.30nama.download/movies/t/The_Huntsman_Winters_War_2016_EXTENDED_Dubbed_1080p_BrRip_30nama_30NAMA.mkv?md5=E38HpAmjkzwU7Fpag-YvtA&expires=1529934194&refresh=4918368251152863819423374231251501'
filename = link[link.rfind('/') + 1:].split('?')[0]
r = requests.get(link, stream=True)
if r.status_code == 200:
with open('test.mkv', 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)