基于Python的一个简单爬虫(糗事百科爬虫)
来源:互联网 发布:数据公司 30亿 收购 编辑:程序博客网 时间:2024/05/16 18:36
# -*- coding: utf-8 -*-import urllib2from bs4 import BeautifulSoupimport threadimport timeclass qiushibaike: """docstring for ClassName""" def __init__(self): self.page = 1 #下载了的页数 self.pages = [] #保存已下载的html self.enable = False #标志位 self.url='http://m.qiushibaike.com/hot/page/' # 用于加载新的页面 def LoadPage(self): # 如果用户未输入quit则一直运行 while self.enable: # 如果pages数组中的内容小于5个 if len(self.pages) < 5: try: # 获取新的页面,加入到数组中 url=self.url+str(self.page) newPage = self.GetHtml(url) self.page += 1 self.pages.append(newPage) except: print '无法链接糗事百科!' else: time.sleep(1) def ParseHtml(self,html): items=self.GetContenBlock(html) for item in items: content=self.ParseContent(item) try: print u"作者",content['author'],u"时间:",content["time"] print content["content"] print '------------------------------------------' except: print u'尼玛这样都有错啊!' #用来获取html def GetHtml(self,url): user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)' headers = { 'User-Agent' : user_agent } req = urllib2.Request(url,None,headers) response = urllib2.urlopen(req) html = response.read() return html def GetContenBlock(self,html): soup = BeautifulSoup(str(html)) items=soup.findAll('div',{'class':'article block untagged mb15'}) return items; def ParseContent(self,item): #soup = BeautifulSoup(str(item)) content=item.find('div',{'class':'content'}) result={} if content!=None: try: result["content"]=content.text.strip() result["time"]=content.get("title").strip() except: result["content"]=None result["time"]=None else: result["content"]=None result["time"]=None #author=item.find('div',{'class':'author clearfix'}) #if author!=None: # result["author"]=author.findAll('a')[1].string #else: # result["author"]=None result['author']=self.ParseAuthor(item) return result; def ParseAuthor(self,item): try: #soup = BeautifulSoup(str(item)) item=item.find('div',{'class':'author clearfix'}) if item!=None: return item.findAll('a')[1].text else: return None; except: return None; def Start(self): self.enable = True page = self.page print u'正在加载中请稍候......' # 新建一个线程在后台加载页面并存储 thread.start_new_thread(self.LoadPage,()) #----------- 加载处理糗事百科 ----------- while self.enable: # 如果self的page数组中存有元素 if self.pages: nowPage = self.pages[0] del self.pages[0] self.ParseHtml(nowPage) page += 1print u"""--------------------------------------- 程序:糗百爬虫 版本:0.1 作者:zz 日期:2013-05-15 语言:Python 2.7 功能:按下回车依次浏览今日的糗百热点---------------------------------------"""print u'请按下回车浏览今日的糗百内容:'raw_input(' ')myModel = qiushibaike()myModel.Start()
0 0
- 基于Python的一个简单爬虫(糗事百科爬虫)
- 一个糗事百科的简单爬虫
- 糗事百科简单爬虫
- python爬虫糗事百科
- Python糗事百科爬虫
- python爬虫“糗事百科”
- python爬虫糗事百科
- python糗事百科爬虫
- python糗事百科爬虫
- 一个爬取糗事百科段子的简单爬虫
- 使用python写糗事百科的爬虫
- 简单爬虫入门:Python 爬虫之糗事百科
- python爬虫(1):糗事百科
- Python爬虫实战:糗事百科
- python爬虫_糗事百科
- [python爬虫] 抓取糗事百科的爬虫程序
- 糗事百科的网络爬虫
- Python 爬虫 简单实例 爬取糗事百科段子
- hdu2099
- 最速下降法/steepest descent,牛顿法/newton,共轭方向法/conjugate direction,共轭梯度法/conjugate gradient 及其他
- IJ整理
- 使用SharedPreferences保存登陆账号和密码
- 装饰者模式
- 基于Python的一个简单爬虫(糗事百科爬虫)
- usaco frac1
- CCProgressTimer 使用
- usaco castle
- make menuconfig/.config/Kconfig解析
- c#中关于udp实现可靠地传输(数据包的分组发送) 升级版
- JAVA学习笔记—review基本知识4
- COCOS2D-X 3.0 Resource下建立一个文件夹,但是文件夹里的资源无法访问
- iOS Storyboard全解析