簡體   English   中英

從URL下載Python文件

[英]Python download file from URL

嘗試從此URL下載文件。 這是一個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)

您應該在get()中將stream設置為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

在這種情況下,使用內置模塊urllib更容易: https : //docs.python.org/2/library/urllib.html#urllib.urlretrieve

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM