【问题标题】:How to donwload xlsx file from a website with Python 3?如何使用 Python 3 从网站下载 xlsx 文件?
【发布时间】:2020-07-31 10:30:29
【问题描述】:

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

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

我尝试了不同的方法(如在 hereenter link description here 等不同的帖子中发现的)。这是我当前代码的几个示例:

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

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

【问题讨论】:

    标签: python-3.x pandas download python-requests xlsx


    【解决方案1】:
    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")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      相关资源
      最近更新 更多