作者:vpoet
日期:大约在夏季
注:代码随意copy 不用告诉我
主要功能:
1.搜索花瓣http://huaban.com/下的图片
2.选定下载图片数目
3.显示下载进度
4.创建目录并下载到桌面
注释少,凑合看。
#coding: utf-8 import urllib, urllib2, re, sys, os import random reload(sys) def Schedule(a,b,c): """a:已经下载的数据块 b:数据块的大小 c:远程文件的大小""" per = 100.0 * a * b / c if per > 100: per = 100 print '%.2f%%' % per def SearchAndDownLoadImg(SearchStr,NumPerPage,filepath): url = 'http://huaban.com/search/?q=%s&per_page=%s' % (SearchStr,str(NumPerPage)) Respon = urllib2.urlopen(url) Htm = Respon.read() print url+"/n/n/n" print "----------------Search Over,And Begin DownLoad----------------"+"/n/n" Patt=re.compile('"file":/{"farm":"farm1",.+?"bucket":"hbimg",.+?"key":"(.*?)",.+?"type":"image/(.*?)",.+?"width":') group = re.findall(Patt,Htm) #print "find total imgurl"+len(group)+"/n" x = 1 for item in group: imgurl=r"http://img.hb.aicdn.com/"+item[0]+"_fw658" urllib.urlretrieve(imgurl,filepath+'/pic%s.%s' % (str(x),item[1]),Schedule) print imgurl+"------>down over" +"/tpic"+ str(x) x = x+1 if __name__ == "__main__": print "Please input the picture you want to download:" SearchStr = raw_input() print "/n/n" print "Please input the PageNumber you want to download:" NumPerPage = raw_input() print "/n/n" print "-----------------------Begin Search---------------------------"+"/n" filenum = random.randint(20, 50) filename = 'PictureFile'+str(filenum) filepath = 'C:/Users/Administrator/Desktop'+'//'+filename if(os.path.exists(filepath) == False): os.mkdir(filepath) #print filepath SearchAndDownLoadImg(SearchStr,NumPerPage,filepath) #http://img.hb.aicdn.com/23a58517fb73f86bca85937f069724486b3e00a44caa-GMc99I_sq75sf print"/n/n" print "---------------------All Down Over-----------------------"
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/8194.html