0

I run the following program:

import urllib.request

def download_web_image(url):
    full_name = "test.pdf"
    urllib.request.urlretrieve(url, full_name)


download_web_image("http://papers.xtremepapers.com/CIE/Cambridge%20IGCSE/Mathematics%20(0580)/0580_s03_qp_1.pdf")

but then get the following error:

File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/urllib/request.py", line 579, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
toasted_flakes
  • 2,502
  • 1
  • 23
  • 38
Z.Chen
  • 385
  • 1
  • 3
  • 13
  • The line of code is jumbled – Z.Chen Jan 21 '16 at 23:17
  • I would guess access to that page is Forbidden, at least the way you are accessing it. I bet some of those questions over on the right sidebar will be helpful. – Dan Jan 21 '16 at 23:20
  • Duplicated : http://stackoverflow.com/questions/13303449/urllib2-httperror-http-error-403-forbidden?rq=1 – Giuseppe Pes Aug 08 '16 at 10:41

1 Answers1

0

try this

import urllib.request

def download_file(url, filename):
    downloaded_file = open(filename, "wb")
    req = urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'})
    file_on_web = urllib.request.urlopen(req)
    print("downloading...")
    while True:
        buf = file_on_web.read(65536)
        if len(buf) == 0:
            break
        downloaded_file.write(buf)
    downloaded_file.close()
    file_on_web.close()
    print("done")


url=r'http://papers.xtremepapers.com/CIE/Cambridge%20IGCSE/Mathematics%20(0580)/058 0_s03_qp_1.pdf'
name = 'test.pdf'
download_file(url,name)
youcef
  • 1
  • 2