python-简单爬虫

 1  #!/usr/bin/python
 2 #coding=utf-8
 3 #网络爬虫
 4 import urllib,re
 5 
 6 def getHtml(url):
 7     page=urllib.urlopen(url)  #打开一个url
 8     html=page.read()   #读取全部内容生成一个字符串(对象)
 9     return html
10 def getImg(html):
11     reg=r'src="(http://.+?\.jpg)"'
12     imgre=re.compile(reg)   #将一个正则表达式生成为响应的对象
13     imglist=re.findall(imgre,html) #从html字符串终止找到符合imgre正则表达式的项,返回列表
14     print imglist
15     x=0
16     for imgurl in imglist:
17         urllib.urlretrieve(imgurl,'%s.jpg'%x)
18         x+=1
19 
20 html=getHtml('http://tieba.baidu.com/p/4710048390')
21 getImg(html)

 

posted @ 2017-07-17 16:30  橙云生  阅读(201)  评论(1编辑  收藏  举报