George Wang
George Wang

Reputation: 43

How to download file from web using request module?

I am using python to download files from webpage, and after I submit the request, I get the headers like below:

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

My question is how could I download the report20160619-013623.csv using the request module.

I search around the web, there are solutions that download the file given a specific URL to that file, however, in my case, the file is generated on web by clicking something like "Download as excel". How do I know where the file is?

Upvotes: 1

Views: 2009

Answers (1)

climmunk
climmunk

Reputation: 1211

You will read the response and then write it out as a file to a location you specify.

csv = r.text

outfile = open('/path/to/my.csv', 'w')
outfile.write(csv)
outfile.close()

Upvotes: 4

Related Questions