python 爬虫小程序

 1 import urllib
 2 import re
 3 
 4 #读取网页内容
 5 def getHtml(url):
 6 
 7     return urllib.urlopen(url).read()
 8 #获取图片
 9 def getImg(html):
10     reg = r'src="(.+?\.jpg)" pic_ext'
11     imgre = re.compile(reg)
12     imagelist = re.findall(imgre,html)
13     x= 0
14     for imgurl in imagelist:
15         urllib.urlretrieve(imgurl,'%s.jpg' % x)
16         x+=1
17         #图片地址
18         print(imgurl)
19 
20 html = getHtml("http://www.baidu.com")
21 
22 getImg(html)

 

posted @ 2015-10-19 20:02  Aeronfay  阅读(195)  评论(1编辑  收藏  举报