dinesh patro
dinesh patro

Reputation: 13

Python download file from URL

Trying to download a file from this URL. It is an excel file and it downloads only 1 kb of it. While the file size is actually 7mb. I dont understand what has to be done here

But if copy and paste the url in IE, the entire file is downloaded

res = requests.get('http://fescodoc.***.com/fescodoc/component/getcontent?objectId=09016fe98f2b59bb&current=true')
res.raise_for_status()
playFile = open('DC_Estimation Form.xlsm', 'wb')
for chunk in res.iter_content(1024):
    playFile.write(chunk)

Upvotes: 0

Views: 4141

Answers (2)

Taku
Taku

Reputation: 33714

You should set stream to true in the get(),

res = requests.get('http://fescodoc.***.com/fescodoc/component/getcontent?objectId=09016fe98f2b59bb&current=true', stream=True)
res.raise_for_status()
with open('DC_Estimation Form.xlsm', 'wb') as playFile:
    for chunk in res.iter_content(1024):
        playFile.write(chunk)

See here: http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow

Upvotes: 1

Eugene Morozov
Eugene Morozov

Reputation: 15816

It is easier to use built-in module urllib for such cases: https://docs.python.org/2/library/urllib.html#urllib.urlretrieve

urllib.urlretrieve('http://fescodoc/component/.', 'DC_Estimation_Form.xslm')

Upvotes: 0

Related Questions