python - 如何使用请求模块从网络下载文件?

标签 python download python-requests

我正在使用 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/

相关文章:

python - IPython 笔记本中的 pprint 和 ipdb

python - 如何检查数组是否在 Python 中的数组列表中?

python - 如何独立于主程序流程触发python方法?

python - libccv - 如何从内存中的字节读取图像

python - 如何编写 Python 脚本从 FTP 服务器下载文件

Python 请求库将 PDF 传递给 Django 服务器

security - 在 Symfony 2 中保护下载链接

r - 为 R 下载 RGoogleDocs(失败)

python - 如何从 instagram 标签查找中获取下一页数据

python - 使用 python requests 下载文件的中间部分