[英]Python: How to download a zip file
我正在嘗試使用以下代碼下載zip文件:
o = urllib2.build_opener( urllib2.HTTPCookieProcessor() )
#login
p = urllib.urlencode( { usernameField: usernameVal, passField: passVal } )
f = o.open(authUrl, p )
data = f.read()
print data
f.close()
#download file
f = o.open(remoteFileUrl)
localFile = open(localFile, "wb")
localFile.write(f.read())
f.close()
我得到一些二進制數據,但我“下載”的文件的大小太小,不是一個有效的zip文件。 我沒有正確檢索zip文件嗎? f = o.open(remoteFileUrl)
的HTTP響應頭如下所示。 我不知道是否需要特殊處理來處理這個響應:
HTTP / 1.1 200 OK服務器:
Apache-Coyote / 1.1 Pragma:私有
緩存控制:必須重新驗證
到期日:1997年12月31日星期二23:59:59 GMT
內容處理:內聯;
文件名= “files.zip”;
內容類型:應用程序/ zip
轉移編碼:分塊
f.read()
不一定讀取整個文件,而只讀取它的一個數據包(如果它很小,可能是整個文件,但不適用於大文件)。
你需要像這樣循環數據包:
while 1:
packet = f.read()
if not packet:
break
localFile.write(packet)
f.close()
f.read()
返回一個空包,表示您已讀取整個文件。
如果你不介意將整個zip文件讀入內存,最快的讀寫方式如下:
data = f.readlines()
with open(localFile,'wb') as output:
output.writelines(data)
否則,當您通過網絡獲取時,要以塊的形式進行讀寫,請執行此操作
with open(localFile, "wb") as output:
chunk = f.read()
while chunk:
output.write(chunk)
chunk = f.read()
這有點不太整潔,但避免將整個文件保存在內存中。 希望能幫助到你。
這是一個更強大的解決方案,使用urllib2以塊的形式下載文件並打印下載狀態
import os
import urllib2
import math
def downloadChunks(url):
"""Helper to download large files
the only arg is a url
this file will go to a temp directory
the file will also be downloaded
in chunks and print out how much remains
"""
baseFile = os.path.basename(url)
#move the file to a more uniq path
os.umask(0002)
temp_path = "/tmp/"
try:
file = os.path.join(temp_path,baseFile)
req = urllib2.urlopen(url)
total_size = int(req.info().getheader('Content-Length').strip())
downloaded = 0
CHUNK = 256 * 10240
with open(file, 'wb') as fp:
while True:
chunk = req.read(CHUNK)
downloaded += len(chunk)
print math.floor( (downloaded / total_size) * 100 )
if not chunk: break
fp.write(chunk)
except urllib2.HTTPError, e:
print "HTTP Error:",e.code , url
return False
except urllib2.URLError, e:
print "URL Error:",e.reason , url
return False
return file
嘗試這個:
#download file
f = o.open(remoteFileUrl)
response = ""
while 1:
data = f.read()
if not data:
break
response += data
with open(localFile, "wb") as local_file:
local_file.write(response)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.