如何使用请求模块从Web下载文件?

时间:2016-06-20 02:35:19

标签: python download python-requests

我正在使用python从网页下载文件,在我提交请求后,我得到如下标题:

>>> 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'}

我的问题是如何使用report20160619-013623.csv模块下载request

我在网上搜索,有一些解决方案可以下载给定该文件的特定URL的文件,但是,在我的情况下,该文件是通过单击“Download as excel”之类的内容在Web上生成的。我怎么知道文件的位置?

1 个答案:

答案 0 :(得分:3)

您将阅读响应,然后将其作为文件写入您指定的位置。

{{1}}