File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ # Source:https://github.com/Show-Me-the-Code/show-me-the-code
2+ # Author:renzongxian
3+ # Date:2014-12-24
4+ # Python 3.4
5+
6+ """
7+
8+ 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-)
9+
10+ """
11+
12+ import urllib .request
13+ import re
14+ import os
15+
16+
17+ def fetch_pictures (url ):
18+ html_content = urllib .request .urlopen (url ).read ()
19+ r = re .compile ('<img pic_type="0" class="BDE_Image" src="(.*?)"' )
20+ picture_url_list = r .findall (html_content .decode ('utf-8' ))
21+
22+ os .mkdir ('pictures' )
23+ os .chdir (os .path .join (os .getcwd (), 'pictures' ))
24+ for i in range (len (picture_url_list )):
25+ picture_name = str (i ) + '.jpg'
26+ try :
27+ urllib .request .urlretrieve (picture_url_list [i ], picture_name )
28+ print ("Success to download " + picture_url_list [i ])
29+ except :
30+ print ("Fail to download " + picture_url_list [i ])
31+
32+ if __name__ == '__main__' :
33+ fetch_pictures ("http://tieba.baidu.com/p/2166231880" )
You can’t perform that action at this time.
0 commit comments