zoukankan      html  css  js  c++  java
  • 【Python/爬虫】一个类化的网络图片批量下载爬虫

    代码:

    #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

  • 相关阅读:
    逆序数———线段树/树状数组
    线段树 模板
    博弈论--对称博弈
    matlab程序设计
    matlab矩阵的操作
    2nd 历年学生作品评论(3部)
    1st 四人小组项目
    1st 本周工作量及进度统计
    1st 结对编程:简易四则运算
    1st 英文文章词频统计
  • 原文地址:https://www.cnblogs.com/heyang78/p/15360062.html
Copyright © 2011-2022 走看看