码迷,mamicode.com
首页 > 编程语言 > 详细

python之花瓣美女下载

时间:2015-06-23 21:42:01      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:python   花瓣   

python之花瓣美女下载


作者: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-----------------------"

运行截图:
搜索Beatuiful,下载20张图片
技术分享
花瓣搜索结果:
技术分享

运行截图,显示下载进度:
技术分享
图片爬取结果:
技术分享


python之花瓣美女下载

标签:python   花瓣   

原文地址:http://blog.csdn.net/u013018721/article/details/46609497

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!