0

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)

2 Answers 2

1

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

0

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')
2
  • Still downloads only 1kb file. Commented Jun 3, 2017 at 6:48
  • Maybe the site sets incorrect Content-Length to fool scraping tools? I'd try using curl -i http://fescodoc.. | less to inspect headers returned by the site first. Also, try setting innocuously-looking User-Agent field, matching, for example, your browser user agent string. Commented Jun 3, 2017 at 8:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.