File tree Expand file tree Collapse file tree 1 file changed +56
-0
lines changed
Expand file tree Collapse file tree 1 file changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ # coding=utf-8
2+
3+ import urllib2
4+ import urllib
5+ import os
6+
7+ from bs4 import BeautifulSoup
8+
9+ """
10+ 0013:用 Python 写一个爬图片的程序,爬 [这个链接里的日本妹子图片 :-)](http://tieba.baidu.com/p/2166231880)
11+ """
12+
13+
14+ def get_img_src_list (url , img_class ):
15+ content = urllib2 .urlopen (url ).read ()
16+
17+ soup = BeautifulSoup (content )
18+ #print soup
19+ src_list = []
20+
21+ for img in soup .find_all ('img' , img_class ):
22+ src_list .append (img ['src' ])
23+
24+ return src_list
25+
26+
27+ def download_img (src , download_path ):
28+ print 'Begin download image : %s ......' % src
29+
30+ file_name = src .split ("/" )[- 1 ]
31+ dist = os .path .join (download_path , file_name )
32+
33+ urllib .urlretrieve (src , dist , None )
34+
35+ print 'Download image %s Done.' % src
36+
37+
38+
39+ if __name__ == '__main__' :
40+ src_list = get_img_src_list ('http://tieba.baidu.com/p/2166231880' , 'BDE_Image' )
41+
42+ if src_list :
43+ save_path = os .path .abspath ("./downlaod" )
44+ if not os .path .exists (save_path ):
45+ os .mkdir (save_path )
46+
47+ print 'Start download %d images.....' % len (src_list )
48+
49+ for src in src_list :
50+ download_img (src , save_path )
51+
52+ print 'Download %d images done.' % len (src_list )
53+ else :
54+ print 'No Imges found !!!'
55+
56+
You can’t perform that action at this time.
0 commit comments