Search code examples
python-3.xpandasdownloadpython-requestsxlsx

How to donwload xlsx file from a website with Python 3?


I am trying to download a file automatically and save it. It is suppossed to be easy but I am founding some difficulties.

In theory is supposed to be easy here, you click automatically you download the file.

I have try different ways (as found in diffeent posts such as here or enter link description here). Here a couple of example of mhy current code:

Option A)

url = "https://www.gov.scot/binaries/content/documents/govscot/publications/statistics/2020/04/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/documents/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/govscot%3Adocument/HSCA%2B-%2BSG%2BWebsite%2B-%2BIndicator%2BTrends%2Bfor%2Bdaily%2Bdata%2Bpublication.xlsx"

response = requests.get(url,stream=False)
with open(dowload_folder_name, 'wb') as out_file:
    shutil.copyfileobj(response.raw, out_file)

Option B)

xl_df = pd.read_excel(url,
                       sheet_name='Table 5 - Testing',
                       skiprows=range(5),
                       skipfooter=0)

In both cases I just get

urllib.error.URLError: <urlopen error [Errno 60] Operation timed out>

Any suggestion, please? Many thanks!


Solution

  • import requests
    
    
    def main(url):
        r = requests.get(url)
        print(r)
        with open("data.xlsx", 'wb') as f:
            f.write(r.content)
    
    
    main("https://www.gov.scot/binaries/content/documents/govscot/publications/statistics/2020/04/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/documents/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/trends-in-number-of-people-in-hospital-with-confirmed-or-suspected-covid-19/govscot%3Adocument/HSCA%2B-%2BSG%2BWebsite%2B-%2BIndicator%2BTrends%2Bfor%2Bdaily%2Bdata%2Bpublication.xlsx")
    

    enter image description here