简体   繁体   English

通过python下载CSV文件(URL)

[英]Download csv file through python (url)

I work on a project and I want to download a csv file from a url. 我正在做一个项目,我想从URL下载一个csv文件。 I did some research on the site but none of the solutions presented worked for me. 我在该站点上进行了一些研究,但所提供的解决方案均不适合我。

The url offers you directly to download or open the file of the blow I do not know how to say a python to save the file (it would be nice if I could also rename it) 该网址可直接为您提供下载或打开文件的文件,我不知道该怎么说一个python保存文件(如果我也可以重命名它会很好)

But when I open the url with this code nothing happens. 但是,当我使用此代码打开url时,没有任何反应。

import urllib
url='https://data.toulouse-metropole.fr/api/records/1.0/download/?dataset=dechets-menagers-et-assimiles-collectes'

testfile = urllib.request.urlopen(url)

Any ideas? 有任何想法吗?

Try this. 尝试这个。 Change "folder" to a folder on your machine 将“文件夹”更改为计算机上的文件夹

import os
import requests

url='https://data.toulouse-metropole.fr/api/records/1.0/download/?dataset=dechets-menagers-et-assimiles-collectes'
response = requests.get(url)
with open(os.path.join("folder", "file"), 'wb') as f:
    f.write(response.content)

You can adapt an example from the docs 您可以根据文档改编示例

import urllib.request
url='https://data.toulouse-metropole.fr/api/records/1.0/download/?dataset=dechets-menagers-et-assimiles-collectes'

with urllib.request.urlopen(url) as testfile, open('dataset.csv', 'w') as f:
    f.write(testfile.read().decode())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM