Rajeev
Rajeev

Reputation: 5012

Error in Downloading live pdf file from URL in python

I am trying to download a pdf file with below Python function. I was able to open that URL(redirect to another URL) in the browser. But the code is getting 404 error.

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")

Upvotes: 1

Views: 859

Answers (1)

Neha Ummareddy
Neha Ummareddy

Reputation: 71

Few websites block based on language or location. Following code with additional header works

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")

Upvotes: 2

Related Questions