我試圖使用wget來下載一個pdf檔案。 我有一個直接鏈接到pdf檔案,并在命令列中輸入以下內容:
wget -A pdf -nc -np -nd --content-disposition --wait=1 --tries=5 "https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf"/span>
這使用了很多不必要的選項,但它們不應該擾亂結果,即:
這使用了很多不必要的選項。
HTTP請求已發送,正在等待回應。讀取錯誤(未知錯誤)in headers。
有沒有辦法直接用wget來解決這個問題,或者有沒有其他的解決方案,最好是Python的,我可以考慮?
uj5u.com熱心網友回復:
你的oneliner對我有用。我已經成功地下載了pdf.
wget -A pdf -nc -np -nd --content-disposition --wait=1 --tries=5 "https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf"/span>
我相信有網路或防火墻問題。
uj5u.com熱心網友回復:
當使用WGET時,它發送它自己的頭資訊,唯一與瀏覽器不同的是用戶代理。
你可以從你的瀏覽器中挑選用戶代理,或者在網上隨便找一個,并在請求程序中把它設定為頭資訊。
uj5u.com熱心網友回復:
下面是一個基于python的解決方案
import requests
url = 'https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf'/span>
r = requests.get(url)
with open('my_file.pdf'/span>, 'wb'/span>) as f:
f.write(r.content)
uj5u.com熱心網友回復:
任何其他的解決方案,最好是在Python中,我可以考慮?
你可以使用urllib.request.urlretrieve從內置模塊urllib.request,如下
import urllib.request
urllib.request.urlretrieve("https://prospektbestellung.nordseetourismus.de/mediafiles/Sonstiges/Ortsprospekte/amrum2021.pdf","amrum2021.pdf")
這段代碼會下載檔案并保存在當前作業目錄下的amrum2021.pdf。與requests不同,urllib.request是內置的模塊,所以除了python本身之外不需要額外安裝。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/311854.html
標籤:
