使用python下载表数据而不提供.csv文件地址

时间:2018-03-08 18:51:58

标签: python download request

我的目的是从这个网站下载数据: http://transoutage.spp.org/

打开此网站时,在网络底部有一个说明用于说明如何自动下载数据。例如: http://transoutage.spp.org/report.aspx?download=true&actualendgreaterthan=3/1/2018&includenulls=true

我写的代码是:

 import requests
 ul_begin = 'http://transoutage.spp.org/report.aspx?download=true'

 timeset = '3/1/2018' #define the time, m/d/yyyy 
 fn = ['&actualendgreaterthan='] + [timeset] + ['&includenulls=true']
 fn = ''.join(fn)
 ul = ul_begin+fn

 r = requests.get(ul, verify=False)

因为,如果输入网址, http://transoutage.spp.org/report.aspx?download=true&actualendgreaterthan=3/1/2018&includenulls=true, 进入Chrome后,它会自动下载.csv文件中的数据。我不知道如何继续我的代码。

请帮帮我!!!!

1 个答案:

答案 0 :(得分:0)

您需要将收到的回复写入文件:

r = requests.get(ul, verify=False)
if 200 >= r.status_code <= 300: 
    # If the request has succeeded
    file_path = '<path_where_file_has_to_be_downloaded>`
    f = open(file_path, 'w+')
    f.write(r.content)
    f.close()

如果csv文件很小,这将正常工作。但对于大文件,您需要使用stream param下载:http://masnun.com/2016/09/18/python-using-the-requests-module-to-download-large-files-efficiently.html