python之花瓣美女下載

python之花瓣美女下載


做者:vpoet
mail:vpoet_sir@163.com
注:代碼隨意copy 不用告訴我

主要功能:
    1.搜索花瓣http://huaban.com/下的圖片
    2.選定下載圖片數目
    3.顯示下載進度
    4.建立目錄並下載到桌面

註釋少,湊合看。
 1 #coding: utf-8
 2 import urllib, urllib2, re, sys, os
 3 import random
 4 reload(sys)
 5  
 6 def Schedule(a,b,c):
 7     """a:已經下載的數據塊
 8     b:數據塊的大小
 9     c:遠程文件的大小"""
10     per = 100.0 * a * b / c
11     if per > 100:
12         per = 100
13     print '%.2f%%' % per
14 
15 
16 
17 
18 
19 def SearchAndDownLoadImg(SearchStr,NumPerPage,filepath):
20     
21     url = 'http://huaban.com/search/?q=%s&per_page=%s' % (SearchStr,str(NumPerPage))
22     
23     Respon = urllib2.urlopen(url)
24     
25     Htm = Respon.read()
26     
27     print url+"\n\n\n"
28     
29     print "----------------Search Over,And Begin DownLoad----------------"+"\n\n"
30     
31     Patt=re.compile('"file":\{"farm":"farm1",.+?"bucket":"hbimg",.+?"key":"(.*?)",.+?"type":"image/(.*?)",.+?"width":')
32     
33     group = re.findall(Patt,Htm)
34     
35     #print "find total imgurl"+len(group)+"\n"
36     
37     x = 1
38     
39     for item in group:
40     
41         imgurl=r"http://img.hb.aicdn.com/"+item[0]+"_fw658"
42         
43         urllib.urlretrieve(imgurl,filepath+'\pic%s.%s' % (str(x),item[1]),Schedule)
44         
45         print imgurl+"------>down over" +"\tpic"+ str(x)
46         
47         x = x+1
48     
49     
50 
51 if __name__ == "__main__":
52     
53     print "Please input the picture you want to download:"
54     
55     SearchStr = raw_input()
56     
57     print "\n\n"
58     
59     print "Please input the PageNumber you want to download:"
60     
61     NumPerPage = raw_input()
62     
63     print "\n\n"
64     
65     print "-----------------------Begin Search---------------------------"+"\n"
66     
67     
68     filenum = random.randint(20, 50)
69     
70     filename = 'PictureFile'+str(filenum)
71     
72     filepath = 'C:\Users\Administrator\Desktop'+'\\'+filename
73     
74     if(os.path.exists(filepath) == False):
75         os.mkdir(filepath)
76     
77     #print filepath
78     
79     SearchAndDownLoadImg(SearchStr,NumPerPage,filepath)
80     
81     #http://img.hb.aicdn.com/23a58517fb73f86bca85937f069724486b3e00a44caa-GMc99I_sq75sf
82     
83     print"\n\n"
84     
85     print "---------------------All Down Over-----------------------"

 


運行截圖:
搜索Beatuiful,下載20張圖片

花瓣搜索結果:


運行截圖,顯示下載進度:

圖片爬取結果:
相關文章
相關標籤/搜索