HTTP 错误 403:使用 urllib 下载文件时禁止

问题描述 投票:0回答:2

我有这行代码:

urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe')
,但是当我运行它时,它会抛出错误
urllib.error.HTTPError: HTTP Error 403: Forbidden

python-3.x file download urllib
2个回答
5
投票

这看起来是一个实际的 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)

0
投票

如果您使用 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)
© www.soinside.com 2019 - 2024. All rights reserved.