BadZipFile(“文件不是zip文件”)

时间:2017-03-24 17:57:08

标签: python api zip

所以我使用提供的代码从api中提取数据:

#User Parameters
        apiToken = apitokens
        surveyId = j
        fileFormat = 'json'

        #Static Parameters
        requestCheckProgress = 0
        baseUrl = 'https://sdfs.asdfs.com/API/v3/responseexports/'
        headers = {
            'content-type': "application/json",
            'x-api-token': apiToken,
            }

        #Creating Data Export

        downloadRequestUrl = baseUrl
        downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '"}'
        downloadRequestResponse = requests.request("POST", downloadRequestUrl, data=downloadRequestPayload, headers=headers)
        progressId = downloadRequestResponse.json()['result']['id']
        print (downloadRequestResponse.text)

        #Checking on Data Export
        while requestCheckProgress < 100:
            requestCheckUrl = baseUrl + progressId
            requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers)
            requestCheckProgress = requestCheckResponse.json()['result']['percentComplete']
            print ("Download is " + str(requestCheckProgress) + " complete")


        #Downloading file
        requestDownloadUrl = baseUrl + progressId + '/file'
        requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True)
        with open('RequestFile.zip', "wb") as f:
            for chunk in requestDownload.iter_content(chunk_size=1024):
                f.write(chunk)
        zipfile.ZipFile('RequestFile.zip').extractall("sdafsadf")

    else:
        print("error")

所以基本上在此代码之前我使用一些嵌套的for循环来为每个调查提供apitokens和surveyid。有超过100项调查被撤回。有时候,我会随便扔掉这个:

zipfile.BadZipFile: File is not a zip file

并且下载表的价格为1350%。我似乎无法找到他们的代码中的错误。我正在使用python 3.6并尝试过使用3.5。

0 个答案:

没有答案