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+ #!/usr/bin/env python3
2+ # -*- coding: utf-8 -*-
3+
4+ '第 0013 题: 用 Python 写一个爬图片的程序,爬(http://tieba.baidu.com/p/2166231880)图片 :-)'
5+
6+ __author__ = 'Drake-Z'
7+
8+ import os
9+ import re
10+ import urllib
11+ from urllib import request
12+ from urllib .request import urlopen
13+
14+ def read_url (yuanshiurl ):
15+ req = request .Request (yuanshiurl )
16+ req .add_header ('User-Agent' , 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET4.0C; .NET4.0E; .NET CLR 2.0.50727; 360SE /360/ /chrome/ig)' )
17+ with request .urlopen (req ) as f :
18+ Imageurl (f .read ().decode ('utf-8' )) #输出Data
19+ return 0
20+
21+ def Imageurl (data ):
22+ re_Imageurl = re .compile (r'src="(http://imgsrc.baidu.com/forum/.*?)"' )
23+ data = re_Imageurl .findall (data ) #输出图片链接
24+ downloadImage (data )
25+
26+ def downloadImage (tupianlianjie ):
27+ dirct = '0013'
28+ try :
29+ if not os .path .exists (dirct ): #创建存放目录
30+ os .mkdir (dirct )
31+ except :
32+ print ('Failed to create directory in %s' % dirct )
33+ exit ()
34+ for i in tupianlianjie :
35+ data = urllib .request .urlopen (i ).read ()
36+ i = re .split ('/' , i )[- 1 ]
37+ print (i )
38+ path = dirct + '/' + i
39+ f = open (path , 'wb' )
40+ f .write (data )
41+ f .close ()
42+ print ('Done !' )
43+
44+ url = 'http://tieba.baidu.com/p/2166231880'
45+ read_url (url )
You can’t perform that action at this time.
0 commit comments