我正在使用 python 从网页下载文件,提交请求后,我得到如下 header :
>>> r.headers
{'Content-Disposition': 'attachment; filename="report20160619-013623.csv";',
'Content-Transfer-Encoding': 'binary', 'Expires': '0',
'Keep-Alive': 'timeout=5, max=100', 'Server': 'Apache',
'Transfer-Encoding': 'chunked', 'Connection': 'Keep-Alive', 'Pragma': 'public',
'Cache-Control': 'must-revalidate, post-check=0, pre-check=0, private',
'Date': 'Sun, 19 Jun 2016 06:35:18 GMT', 'X-Frame-Options': 'SAMEORIGIN',
'Content-Type': 'application/octet-stream'}
我的问题是如何使用 request
模块下载 report20160619-013623.csv
。
我在网络上搜索,有一些解决方案可以下载给定该文件的特定 URL 的文件,但是,就我而言,该文件是通过单击“下载为 Excel”之类的内容在网络上生成的。我如何知道文件在哪里?
最佳答案
您将读取响应,然后将其作为文件写入您指定的位置。
csv = r.text
outfile = open('/path/to/my.csv', 'w')
outfile.write(csv)
outfile.close()
关于python - 如何使用请求模块从网络下载文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37913921/