我的目的是从这个网站下载数据: 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文件中的数据。我不知道如何继续我的代码。
请帮帮我!!!!
答案 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