0

I want to download a file like this:

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

I've added headers exactly like this post, but the result is:

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

What should I do?

amin
  • 1,413
  • 14
  • 24
ehsan shirzadi
  • 4,709
  • 16
  • 69
  • 112

1 Answers1

1

Due to this answer, 30nama website blocks non-browser agents. You can try stream requests instead.

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)
amin
  • 1,413
  • 14
  • 24