我正在尝试从以下 URL 下载文件:
https://www.cmegroup.com/content/dam/cmegroup/notices/clearing/2020/08/Chadv20-239.pdf
我尝试使用 python requests 库,但请求超时。我尝试将浏览器中的“用户代理”指定为 header ,但它仍然超时,包括当我将浏览器中的每个 header 复制到 python 脚本中时。我尝试设置allow_redirects = True,这没有帮助。我也尝试过 wget 和curl,除了实际打开浏览器、访问 URL 和下载文件之外,一切都失败了。
我想知道浏览器中的请求和我设置 header 以匹配浏览器中的请求的 python 请求之间的实际区别是什么 - 有什么方法可以使用 python 下载此文件吗?
代码片段:
import requests
requests.get("https://www.cmegroup.com/content/dam/cmegroup/notices/clearing/2020/08/Chadv20-239.pdf") # hangs
最佳答案
检查一下,它对我有用。
import requests
headers = {
"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'}
response = requests.get(
"https://www.cmegroup.com/content/dam/cmegroup/notices/clearing/2020/08/Chadv20-239.pdf", headers=headers)
pdf = open("Chadv20-239.pdf", 'wb')
pdf.write(response.content)
pdf.close()
关于python - 无法使用 python 从 URL 下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70361552/