代码:
#encoding=utf-8 import urllib.request import os class WebPicDownloader: def __init__(self,path,start,end,extension,folder): self.folder=folder os.makedirs(folder) # 创建目录 self.fileUrls={} # 用一个字典去存文件名和地址 for i in range(start,end+1): name=str(i).zfill(2) #string.zfill(n)是字符串的左补零函数 fileName=name+extension picUrl=path+fileName self.fileUrls[fileName]=picUrl print('要下载的图片地址列表装填完毕,共有'+str(len(self.fileUrls))+'张图片.') self.batchDownload() def batchDownload(self): print('开始逐个下载图片.') for file,url in self.fileUrls.items(): self.downloadOne(file,url) #self.method 调用本类方法 def downloadOne(self,file,url): with urllib.request.urlopen(url) as response: data=response.read() filePathName=self.folder+"/"+file with open(filePathName,'wb') as f: f.write(data) print('从:'+url+'下载的文件已存为文件:'+filePathName) dld=WebPicDownloader("https://tu.gtmm.net:8988/listImg/2020/07/07/107/",1,20,".jpg","xyyx01")
这个爬虫针对的地址是:https://www.gtmm.net/rhmn/list_5_12.html ,灵泛的同学观察出规律就可以去批量下载了。
END