Wget 无法直接从 link 下载 pdf

Wget fails to download a pdf from a direct link

我正在尝试使用 wget 下载 pdf 文件。 我有一个 direct link 到 pdf 文档并在命令行中输入以下内容:

wget -A pdf -nc -np -nd --content-disposition --wait=1 --tries=5 "https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf"

这使用了很多不必要的选项,但它们不应该影响结果,即:

HTTP request sent, awaiting response... Read error (Unknown error) in headers.

有没有什么方法可以直接使用 wget 解决这个问题,或者有任何其他解决方案,最好是 Python,我可以考虑吗?

使用 WGET 时,它会发送自己的 headers,唯一与浏览器不同的是 user-agent.

您可以从浏览器中选择 user-agent 或只是在线随机获取一个并在请求期间将其设置为 header。

你的 oneliner 适合我。我已经成功下载pdf了。

wget -A pdf -nc -np -nd --content-disposition --wait=1 --tries=5 "https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf"

我认为是网络或防火墙问题。

下面是一个基于python的解决方案

import requests

url = 'https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf'
r = requests.get(url)
with open('my_file.pdf', 'wb') as f:
    f.write(r.content)

我可以考虑的任何其他解决方案,最好是 Python?

您可以使用内置模块 urllib.request 中的 urllib.request.urlretrieve,如下所示

import urllib.request
urllib.request.urlretrieve("https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf","amrum2021.pdf")

此代码会下载文件并将其保存在当前工作目录中的名称 amrum2021.pdf 下。与 requests 不同,urllib.request 是内置模块,因此不需要 python 本身以外的额外安装。