【问题标题】:Downloading a file with a URL using python使用python下载带有URL的文件
【发布时间】:2021-02-14 12:06:34
【问题描述】:

我想使用 python 下载以下 url 中的文件。我尝试使用以下代码,但似乎无法正常工作。我认为错误在于文件格式。如果您能建议修改代码或我可以用于此目的的新代码,我会很高兴

网站链接

https://www.gov.uk/government/statistics/transport-use-during-the-coronavirus-covid-19-pandemic

需要下载的网址

https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/959864/COVID-19-transport-use-statistics.ods

我的代码

from urllib import request


response = request.urlopen("https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/959864/COVID-19-transport-use-statistics.ods")
csv = response.read()


csvstr = str(csv).strip("b'")

lines = csvstr.split("\\n")
f = open("historical.csv", "w")
for line in lines:
   f.write(line + "\n")
f.close()

这里基本上我只想下载文件。我听说可以使用 Beautifulsoup,但我对此没有太多经验。非常感谢任何可以满足我的目的的代码

谢谢

【问题讨论】:

  • “它似乎不工作” - 究竟如何它是“不工作”?
  • 数据经过编码,在csv文件中不显示确切内容

标签: python url beautifulsoup


【解决方案1】:

下载文件:

In [1]: import requests

In [2]: url = 'https://assets.publishing.service.gov.uk/government/uploads/syste
   ...: m/uploads/attachment_data/file/959864/COVID-19-transport-use-statistics.
   ...: ods'

In [3]: with open('COVID-19-transport-use-statistics.ods', 'wb') as out_file:
   ...:     content = requests.get(url, stream=True).content
   ...:     out_file.write(content)

然后你可以使用pandas-ods-reader通过运行来读取文件:

pip install pandas-ods-reader

然后:

In [4]: from pandas_ods_reader import read_ods

In [5]: df = read_ods('COVID-19-transport-use-statistics.ods', 1)

In [6]: df
Out[6]: 
                   Department for Transport statistics  ...   unnamed.9
0    https://www.gov.uk/government/statistics/trans...  ...        None
1                                                 None  ...        None
2    Use of transport modes: Great Britain, since 1...  ...        None
3    Figures are percentages of an equivalent day o...  ...        None
4                                                 None  ...  Percentage
..                                                 ...  ...         ...
390                  Transport for London Tube and Bus  ...        None
391                               Buses (excl. London)  ...        None
392                                           Cycling   ...        None
393                                  Any other queries  ...        None
394                                    Media enquiries  ...        None

如果这是你想要的,你可以使用df.to_csv('my_data.csv', index=False)将其保存为csv

【讨论】:

  • 谢谢!! .所以继续不给出.ods文件的URL,我们可以从网站的URL下载这个文件
  • 是的,这可以通过使用 xpath 或 BeautifulSoup 从网站获取目标 url 来完成,然后执行我提到的完全相同的步骤。
  • 我试过bs4,它可以工作。现在我在读取 ods 文件时遇到问题,因为我下载的 ods 文件中有一个标题(前 6 行),数据帧没有正确解释。你能建议一个可以做的修改吗
  • not properly interpreted 是什么意思?
  • 列应该是Date, Cars, Light Commercial Vehicles......但是由于.ods文件在前6行包含一个标题,所以创建的列是不同的
【解决方案2】:

我看到您只是想下载.ods 格式的文件,我认为将其保存为.csv 不会将其转换为csv 文件。

以下代码将帮助您下载文件。我使用了requests 库,它是代替 urllib 的更好选择。

import requests

file_url = "https://assets.publishing.service.gov.uk/government/uploads/system/uploads/attachment_data/file/959864/COVID-19-transport-use-statistics.ods"


file_data = requests.get(file_url).content
# create the file in write binary mode, because the data we get from net is in binary
with open("historical.ods", "wb") as file:
    file.write(file_data)

输出文件可以在 MS Excel 中查看。

【讨论】:

  • 谢谢!! .所以继续不给出.ods文件的URL,我们可以从网站的URL下载这个文件
  • 是的,你需要beautifulsoup。无需提供文件 url,而是提供网站 url,它将返回 html 内容,然后使用 BS 库,您可以获取将在页面上某处编码的文件的 url,然后其余代码下载它。
  • 你能提供我的代码吗?这是 URL gov.uk/government/statistics/…> 我想只使用这个 URL 下载上面的文件
  • 我试过bs4,它可以工作。现在我在读取 ods 文件时遇到问题,因为我下载的 ods 文件中有一个标题(前 6 行),数据帧没有正确解释。你能建议一个可以做的修改吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 2020-11-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多