来自网址

时间:2017-06-03 06:40:27

标签: python python-requests urllib

尝试从此网址下载文件。它是一个excel文件,它只下载1 kb。而文件大小实际上是7mb。我不明白这里要做什么

但是如果在IE中复制并粘贴网址,则会下载整个文件

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 个答案:

答案 0 :(得分:1)

您应该在stream

中将get()设置为true
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)

见这里:http://docs.python-requests.org/en/latest/user/advanced/#body-content-workflow

答案 1 :(得分:0)

在这种情况下使用内置模块urllib会更容易:https://docs.python.org/2/library/urllib.html#urllib.urlretrieve

urllib.urlretrieve('http://fescodoc/component/.', 'DC_Estimation_Form.xslm')
相关问题