如何从 Python 3 的网站下载 xlsx 文件?

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

我正在尝试自动下载文件并保存。本来应该很简单,但我发现了一些困难。

理论上应该很容易here,你点击自动下载文件。

我尝试了不同的方法(在 here or 等不同的帖子中找到)。这里有几个当前代码的例子:

选项 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)

选项 B)

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

在这两种情况下我都得到

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

有什么建议吗? 非常感谢!

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