urllib.urlretrieve下载空的zip文件

时间:2017-06-19 12:39:26

标签: python python-2.7 urllib confluence confluence-rest-api

我正在尝试下载zip文件中的空间导出。但不知何故,python下载了一个空的和损坏的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()

1 个答案:

答案 0 :(得分:2)

这是通过Coldspeed的答案解决的。更改以下内容:

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