[python爬虫] 抓取糗事百科的爬虫程序
2015-07-24 10:29
501 查看
抓取糗事百科的爬虫程序
先贴上代码,等假期回家了把过程写一写# -*- coding:utf-8 -*- import re import urllib2 page = 1 url = 'http://www.qiushibaike.com/hot/page/' + str(page) user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } try: request = urllib2.Request(url,headers = headers) response = urllib2.urlopen(request) # print response.read() except urllib2.URLError, e: if hasattr(e,"code"): print e.code if hasattr(e,"reason"): print e.reason # pattern = re.compile('<div class="author".*?>.*?<a.*?>.*?<img.*?/>(.*?)</a>.*?</div>',re.S) # 该表达式可以匹配出作者 pattern = re.compile('<div class="author".*?>.*?<a.*?>.*?<img.*?/>(.*?)</a>.*?</div>.*?<div class="content">(.*?)<!--(.*?)-->.*?</div>',re.S) content = response.read().decode('utf-8') items = re.findall(pattern,content) for i in items: print '<<<'+'-'*60+'>>>' print 'author:'+ i[0].strip() print 'content:'+ i[1].strip() print 'time:'+ i[2].strip() print '\n'
相关文章推荐
- Python-常用库扩展
- ac53 python中文输入输出在windows下的cmd中的实现
- VELT-0.1.5开发:在VS2013下进行python开发
- python 例子1
- python 例子1
- python 例子1
- python 例子1
- python 例子1
- 初识python django, 让我们一起进入python的世界吧
- Python 双引号 单引号 多引号区别
- Python的Django框架中模板碎片缓存简介
- 每个 Python 程序员都要知道的日志实践
- Python中异常处理机制
- Python中的变量有作用域
- Python-WSGI详解汇总
- Python的Django框架中模板碎片缓存简介
- 浅谈Python的Django框架中的缓存控制
- 详解Python的Django框架中的中间件
- Python字符串转换成浮点数函数分享
- 将Python的Django框架与认证系统整合的方法