Python 3.4.2 urllib错误:urllib.error.HTTPError:HTTP错误403:禁止

时间:2016-01-21 23:13:35

标签: python-3.x urllib

我运行以下程序:

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

但后来出现以下错误:

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

1 个答案:

答案 0 :(得分:0)

试试这个

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)