码迷,mamicode.com
首页 > 编程语言 > 详细

python-简单爬虫

时间:2017-07-17 17:16:15      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:bsp   内容   列表   读取   pytho   div   正则表达式   import   int   

 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=rsrc="(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)

 

python-简单爬虫

标签:bsp   内容   列表   读取   pytho   div   正则表达式   import   int   

原文地址:http://www.cnblogs.com/chengyunshen/p/7196117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!