Skip to content

Commit 2155c8f

Browse files
committed
0013 Done.
1 parent af8bccd commit 2155c8f

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

ddkangfu/0013/0013.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+

0 commit comments

Comments
 (0)