2015-10-19 121 views
0

我是Python新手。这里是我的环境设置:使用python下载csv文件3

我有Anaconda 3(Python 3)。我希望能够从网站上下载CSV文件: https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD

我想使用请求库。我将不胜感激任何帮助,以确定我如何使用请求库将CSV文件下载到本机上的本地目录

+0

你有没有找到更好的lution?我相信您可以调整缓冲区的大小以获得更好的获取性能。 – apast

回答

1

建议将数据下载为流,并将其刷新到目标或中间本地文件中。

import requests 


def download_file(url, output_file, compressed=True): 
    """ 
    compressed: enable response compression support 
    """ 
    # NOTE the stream=True parameter. It enable a more optimized and buffer support for data loading. 
    headers = {} 
    if compressed: 
     headers["Accept-Encoding"] = "gzip" 

    r = requests.get(url, headers=headers, stream=True) 

    with open(output_file, 'wb') as f: #open as block write. 
     for chunk in r.iter_content(chunk_size=4096): 
      if chunk: # filter out keep-alive new chunks 
       f.write(chunk) 
     f.flush() #Afterall, force data flush into output file (optional) 

    return output_file 

考虑原帖:

remote_csv = "https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv?accessType=DOWNLOAD" 
local_output_file = "test.csv" 

download_file(remote_csv, local_output_file) 

#Check file content, just for test purposes: 
print(open(local_output_file).read()) 

基本码的这个帖子提取:https://stackoverflow.com/a/16696317/176765

在这里,你可以对身体流的使用更详细的信息与请求的lib:

http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow

+1

在这里感谢我使用的代码。这可能听起来很基本,并希望任何更改/更新,使其更好:导入请求 r = requests.get(“https://data.baltimorecity.gov/api/views/dz54-2aru/rows.csv? ('test.csv','wb')作为f: f.write(r.content) – user3049935

+0

考虑编辑后的文章作为一般下载目的代码。您可以使用任何内容格式。 – apast