Search code examples
pythoncurlpython-requestsurllib2

Download a binary file using Python requests module


I need to download a file from an external source, I am using Basic authentication to login to the URL

import requests
response = requests.get('<external url', auth=('<username>', '<password>'))
data = response.json()
html = data['list'][0]['attachments'][0]['url']
print (html)
data = requests.get('<API URL to download the attachment>', auth=('<username>', '<password>'), stream=True)
print (data.content) 

I am getting below output

<url to download the binary data> 
\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0f\xcb\x00\x00\x1e\x00\x1e\x00\xbe\x07\x00\x00.\xcf\x05\x00\x00\x00'

I am expecting the URL to download the word document within the same session.


Solution

  • When you want to download a file directly you can use shutil.copyfileobj():

    https://docs.python.org/2/library/shutil.html#shutil.copyfileobj

    You already are passing stream=True to requests which is what you need to get a file-like object back. Just pass that as the source to copyfileobj().