【问题标题】:IOError: ('http error', 403, 'Forbidden', <httplib.HTTPMessage instance at 0x7f98ec3d92d8>)IOError: ('http error', 403, 'Forbidden', <httplib.HTTPMessage instance at 0x7f98ec3d92d8>)
【发布时间】:2018-06-11 14:29:08
【问题描述】:

我想下载这样的文件:

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')

我添加了与 post 完全一样的标题, 但结果是:

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>)

我该怎么办?

【问题讨论】:

    标签: python python-2.7 urllib


    【解决方案1】:

    由于这个answer,30nama 网站阻止了非浏览器代理。 你可以试试直播requests

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 2022-12-19
      • 2017-01-14
      • 2022-12-04
      • 2019-05-26
      相关资源
      最近更新 更多