我运行以下程序:
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
答案 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)