2017-06-19 528 views
1

我試圖下載zip文件中的空間導出。但不知何故python下載一個空的和損壞的zip文件。當你通過瀏覽器下載文件手冊時,一切正常。urllib.urlretrieve下載空zip文件

我使用Python 2.7.13

#!/usr/bin/python 
import xmlrpclib 
import time 
import urllib 

confluencesite = "https://confluence.com" 
server = xmlrpclib.ServerProxy(confluencesite + '/rpc/xmlrpc') 
username = '*' 
password = '*' 
token = server.confluence2.login(username, password) 
loginString = "?os_username=" + username + "&os_password=" + password 
filelist = "" 
start = True 
spacesummary = server.confluence2.getSpaces(token) 
for space in spacesummary: 
    #if space['name'] == "24-codING": 
    # start = True 
    # continue 
    if start: 
     if space['type'] == 'global': 
      print "Exporting space " + space['name'] 
      spaceDownloadUrl = server.confluence2.exportSpace(token, space['key'], 
                   "TYPE_XML", 
                   exportAll['true']) 
      filename = spaceDownloadUrl.split('/')[-1].split('#')[0].split('?')[0] 
      time.sleep(0.5) 
      urllib.urlretrieve(spaceDownloadUrl + loginString, filename) 
      print filename + " saved." 
      f = open("exportedspaces.txt", 'a') 
      f.write(filename + "\n") 
      f.close() 
+0

我在代碼中看不到任何zipfile;它應該是'f = open(「exportedspaces.txt」,'a')'? – snakecharmerb

+0

@snakecharmerb zipfile由exportSpace API操作生成,該操作可創建可下載的zip文件。例如:Confluence-space-export-122851-90.xml.z –

+1

可能應該將'&'更改爲'&'。 –

回答

2

它由Coldspeed的答案來解決。更改以下內容:

loginString = "?os_username=" + username + "&os_password=" + password 
to 
loginString = "?os_username=" + username + "&os_password=" + password 
+2

孩子們,一定要清理你的網址參數。 OP,請標記你的答案接受並關閉這個問題。乾杯。 –