Python 3.4.3批量下载图片
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2019-05-28 13:28:54
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
有时候我们手边会整理出一些需要下载的文件图片等地址,下面提供一个指下载的python脚本
import urllib.request import sys import os import socket socket.setdefaulttimeout(60) #创建目录 def create_dir(dirpath): if not os.path.exists(dirpath): os.makedirs(dirpath) class kl_download(object): """docstring for download""" def __init__(self, arg=None): super(kl_download, self).__init__() self.arg = arg def callbackfunc(self,blocknum, blocksize, totalsize): '''回调函数 @blocknum: 已经下载的数据块 @blocksize: 数据块的大小 @totalsize: 远程文件的大小 ''' global url percent = 100.0 * blocknum * blocksize / totalsize if percent >= 100: percent = 100 downsize=blocknum * blocksize if downsize >= totalsize: downsize=totalsize if __name__ == "__main__": s ="%.2f%%=>%.2f B/%.2f B \r"%(percent,downsize,totalsize) sys.stdout.write(s) sys.stdout.flush() if percent == 100: print('') return True def downfile(self,url,outdir='',outfilename=''): if not outfilename: outfilename=os.path.basename(url) try: create_dir(outdir) filepath=outdir+'/'+outfilename urllib.request.urlretrieve(url, filepath, self.callbackfunc) return filepath except Exception as e: print(e) return None def downimage(self,url,outdir='./',outfilename=''): if not outfilename: outfilename=os.path.basename(url) try: create_dir(outdir) filepath=outdir+'/'+outfilename opener=urllib.request.build_opener() req=urllib.request.Request(url) req.add_header('Referer',url) req.add_header('User-Agent','Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0;') r=opener.open(req) #写文件到本地 respHtml = r.read(); binfile = open(filepath, "wb"); binfile.write(respHtml); binfile.close(); return filepath except Exception as e: print(e) return None if __name__ == "__main__": download=kl_download() s='http://i.gtimg.cn/qzone/space_item/orig/8/116120_top.jpg,http://i.gtimg.cn/qzone/space_item/orig/13/116125_top.jpg,http://i.gtimg.cn/qzone/space_item/orig/3/115283_top.jpg,http://i.gtimg.cn/qzone/space_item/orig/15/115279_top.jpg' outdir="./downs/pifu" a=s.split(',') for i in a: print(i+"\n") download.downfile(i,outdir) print('下载完毕') os.system("pause")