Python:批量下载xml文件会返回损坏的zip文件

时间:2017-05-11 07:14:09

标签: python xml windows batch-file urllib2

绘制灵感from this post,我试图从网站批量下载一堆xml个文件:

import urllib2

url='http://ratings.food.gov.uk/open-data/'

f = urllib2.urlopen(url)
data = f.read()
with open("C:\Users\MyName\Desktop\data.zip", "wb") as code:
    code.write(data)

zip文件是在几秒钟内创建的,但是当我尝试访问它时,会出现一个错误窗口:

Windows cannot open the folder.
The Compressed (zipped) Folder "C:\Users\MyName\Desktop\data.zip" is invalid.

我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

您无法将其编码为zip文件。如果您选择在纯文本编辑器(如记事本)中打开它,它应该显示原始xml。

答案 1 :(得分:1)

您没有在zip文件中打开文件句柄:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main {

    int i, s1, s2, j, a[5][5];

    srand (time (NULL));

    printf ("The matrix is:\n");

    for (i = 0; i < 5; i++) {
        printf ("\n\n");
        for (j = 0; j < 5; j++) {
            *(*(a + i) + j) = rand ();
            printf ("%d ", *(*(a + i) + j));
        }
    } 

    for (i = 0; i < 5; i++) {
        s1 += a[i][i];          // main diagonal
        s2 += a[i][4 - i];      // second diagonal
    }

    printf ("\n\nThe sum 1:%d\nThe sum 2:%d", s1, s2);

    if (s1 == s2) {
        printf ("They are the same");
    }

    return 0;
}