在python中从URL下载实时pdf文件时出错

问题描述 投票:1回答:1

我想下载一个低于Python函数的pdf文件。我能够在浏览器中打开URL(重定向到另一个URL)。但是代码出现了404错误。

import requests
 def downloadFile(url, fileName):
        r = requests.get(url, allow_redirects=True, stream=True)
        with open(fileName, "wb") as pdf:             
            for chunk in r.iter_content(chunk_size=1024):
                if chunk:
                    pdf.write(chunk)


downloadFile("http://pubs.vmware.com/vsphere-55/topic/com.vmware.ICbase/PDF/vsphere-esxi-vcenter-server-552-storage-guide.pdf", "vsphere-esxi-vcenter-server-552-storage-guide.pdf")
python python-requests
1个回答
2
投票

很少有网站根据语言或位置进行阻止。以下代码与附加标题工作

In [11]: def downloadFile(url, fileName):
         headers = {'Accept-Language': 'en-US,en;q=0.9,te;q=0.8'}
         r = requests.get(url, allow_redirects=True, stream=True, headers=headers)
        with open(fileName, "wb") as pdf:             
           for chunk in r.iter_content(chunk_size=1024):
               if chunk:
                    pdf.write(chunk)

In [12]: downloadFile("http://pubs.vmware.com/vsphere-55/topic/com.vmware.ICbase/PDF/vsphere-esxi-vcenter-server-552-storage-guide.pdf", "vsphere-esxi-vcenter-server-552-storage-guide.pdf")
© www.soinside.com 2019 - 2024. All rights reserved.