python3,如何下载zip文件

时间:2019-08-07 10:21:52

标签: python

我有一个简单的脚本,可以将zip文件下载到该文件夹​​中,我不知道自己在做什么,文件总是损坏,我附上了一个可复制的示例,首先我得到了文件列表,效果很好,但是当我下载这些文件时,它抱怨zip损坏。

import re ,shutil
from urllib.request import urlopen
url = "http://nemweb.com.au/Reports/Current/Dispatch_SCADA/"
result = urlopen(url).read().decode('utf-8')
pattern = re.compile(r'[\w.]*.zip')
filelist = pattern.findall(result)


for x in filelist:
     with urlopen(url) as source, open(x, 'w+b') as target:
          shutil.copyfileobj(source, target)

1 个答案:

答案 0 :(得分:0)

您始终对HTML页面使用相同的url

您必须下载url + "/" + x

 for x in filelist:
     with urlopen(url + "/" + x) as source, open(x, 'w+b') as target:
         shutil.copyfileobj(source, target)