1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#coding:utf-8
#爬虫基础,需要两个模块urllib和re
import  urllib,re
 
#获取网页源码
def  get_html():
     page  =  urllib.urlopen( 'http://www.baidu.com' )
     html  =  read(page)   #用read方式读取网页源代码
     return  html
x = 0
#匹配url的图片地址,然后下载   
def  getimages():
     #编译成正则表达式对象,compile提高效率
     image_re = re. compile (r 'src="(.*?)" class=' )
     
     #找到re匹配的所有字串,通过列表返回
     image_list  =  re.findall(image_re,html)
     for  image_url  in  image_list:
         print  image_url
         global  #全局变量,后面可以跟上一个或多个变量
         
         #将url定位到的html下载到本地
         urllib.urlretrieve(image_url, '/tmp/python/%s.jpg' % x)