• 【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

  • 相关阅读:
    swift init继承问题
    CocoaPods 使用本地代码
    关于Xcode6 Segue 的疑问,没有解决!
    Cocos2d 学习资料推荐
    iOS8中 UILocalNotification 和 UIRemoteNotification 使用注意
    Cocos2d 初学基本知识
    iOS 和 Android 触摸事件传递
    iOS NSOperation的使用
    Android 相机对焦模式
    AES 推荐文章
  • 原文地址:https://www.cnblogs.com/heyang78/p/15360062.html
Copyright © 2020-2023  润新知