我有这行代码:
urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe')
,但是当我运行它时,它会抛出错误urllib.error.HTTPError: HTTP Error 403: Forbidden
。
这看起来是一个实际的 HTTP
403: Forbidden
错误。 Python urllib
在遇到 HTTP 状态代码时抛出异常(记录于here)。 403
一般意味着:“服务器理解请求,但拒绝满足它。”您需要添加 HTTP 标头来识别自己的身份并避免 403
错误,有关 Python urllib headers 的文档。这是使用 urlopen
的示例:
import urllib.request
req = urllib.request.Request('http://lolupdater.com/downloads/LPB.exe', headers={'User-Agent': 'Mozilla/5.0'})
response = urllib.request.urlopen(req)
对于 Python 3
urllib.urlretrieve()
是 被认为是遗产。为此,我会推荐Python Requests,这是一个工作示例:
import requests
url = 'http://lolupdater.com/downloads/LPB.exe'
r = requests.get(url)
with open('LPBtest.exe', 'wb') as outfile:
outfile.write(r.content)
如果您使用 urllib 下载 pdf,您可能会因为没有指定标头而收到 403 错误。要解决此问题并下载任何带有链接的 pdf 文件,请使用:
headers={"User-Agent": "_ANY_USER"}
pdf_response = requests.get(pdf_url, headers=headers)
with open(f'C:/Users/username/path/{file_name}.pdf',
'wb') as f:
f.write(pdf_response.content)