2017-08-26 39 views
0

當我保存this鏈接通過我的firefox內置的下載器,它給了我一個可用的xlsx文件,我可以通過excel或libreoffic打開。但是當我嘗試通過aria2c或urllib.request.urlretrieve在python代碼中下載它時遇到保存的文件無法使用的問題。爲什麼這個問題發生?你能給我一個python代碼來存儲上面的鏈接中可用的xlsx文件嗎?在python和firefox中保存頁面爲xlsx

+1

我建議你編輯問題,包括你迄今爲止嘗試過的代碼。 –

回答

0

下面的方法應該做你需要的東西:

import requests 
import re 
import os 

xlsx_req = requests.get("http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0") 
xlsx_filename = os.path.basename(re.findall("filename=(.+)", xlsx_req.headers['content-disposition'])[0]) 

with open(xlsx_filename, 'wb') as f_xlsx: 
    f_xlsx.write(xlsx_req.content) 

這使用要求下載該文件。它還從content-disposition標題中提取要使用的文件名。