我正在尝试使用 requests.get() 下载 PDF 文件。它适用于我发现的大多数测试 PDF 文件,但对于本例则不起作用,并且文件已损坏。如果我用浏览器打开 URL 并保存文件,它就可以正常工作。我尝试使用“Stream”分块下载它,但结果相同。你能向我解释一下我错过了什么吗?
import requests
file_url = 'http://medianet.edmond-de-rothschild.fr/edram/pdf/kiid_fr0010172767_en_20200120_20200128_1954.pdf'
headers = {'Content-type': 'application/pdf'}
r = requests.get(file_url, headers=headers)
with open("python.pdf", "wb") as pdf:
pdf.write(r.content)
pdf.close()
修复header信息使其发挥作用。
import requests
file_url = "http://medianet.edmond-de-rothschild.fr/edram/pdf/kiid_fr0010172767_en_20200120_20200128_1954.pdf"
headers = {
"User-Agent": "PostmanRuntime/7.20.1",
"Accept": "*/*",
"Cache-Control": "no-cache",
"Postman-Token": "8eb5df70-4da6-4ba1-a9dd-e68880316cd9,30ac79fa-969b-4a24-8035-26ad1a2650e1",
"Host": "medianet.edmond-de-rothschild.fr",
"Accept-Encoding": "gzip, deflate",
"Connection": "keep-alive",
"cache-control": "no-cache",
}
r = requests.get(file_url, file_url, headers=headers)
with open("python.pdf", "wb") as pdf:
pdf.write(r.content)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)