zoukankan      html  css  js  c++  java
  • python 爬取百度贴吧

    # -*- coding:utf-8 -*-
    import urllib
    import urllib2
    import re
    #处理页面标签类
    class Tool:
        #去除img标签,7位长空格
        removeImg = re.compile('<img.*?>| {7}|')
        #删除超链接标签
        removeAddr = re.compile('<a.*?>|</a>')
        #把换行的标签换为
    
        replaceLine = re.compile('<tr>|<div>|</div>|</p>')
        #将表格制表<td>替换为	
        replaceTD= re.compile('<td>')
        #把段落开头换为
    加空两格
        replacePara = re.compile('<p.*?>')
        #将换行符或双换行符替换为
    
        replaceBR = re.compile('<br><br>|<br>')
        #将其余标签剔除
        removeExtraTag = re.compile('<.*?>')
        def replace(self,x):
            x = re.sub(self.removeImg,"",x)
            x = re.sub(self.removeAddr,"",x)
            x = re.sub(self.replaceLine,"
    ",x)
            x = re.sub(self.replaceTD,"	",x)
            x = re.sub(self.replacePara,"
        ",x)
            x = re.sub(self.replaceBR,"
    ",x)
            x = re.sub(self.removeExtraTag,"",x)
            #strip()将前后多余内容删除
            return x.strip()
    
    #百度贴吧爬虫类
    class BDTB:
    
        #初始化,传入基地址,是否只看楼主的参数
        def __init__(self,baseUrl,seeLZ,floorTag):
            #base链接地址
            self.baseURL = baseUrl
            #是否只看楼主
            self.seeLZ = '?see_lz='+str(seeLZ)
            #HTML标签剔除工具类对象
            self.tool = Tool()
            #全局file变量,文件写入操作对象
            self.file = None
            #楼层标号,初始为1
            self.floor = 1
            #默认的标题,如果没有成功获取到标题的话则会用这个标题
            self.defaultTitle = u"百度贴吧"
            #是否写入楼分隔符的标记
            self.floorTag = floorTag
            
    
        #传入页码,获取该页帖子的代码
        def getPage(self,pageNum):
            try:
                url = self.baseURL+ self.seeLZ + '&pn=' + str(pageNum)
                request = urllib2.Request(url)
                response = urllib2.urlopen(request)
                return response.read().decode('utf-8')
            except urllib2.URLError, e:
                if hasattr(e,"reason"):
                    print u"连接百度贴吧失败,错误原因",e.reason
                    return None
        
        #获取帖子标题
        def getTitle(self,page):
            pattern = re.compile('<h3 class="core_title_txt.*?>(.*?)</h3>',re.S)
            result = re.search(pattern,page)
            if result:
                return result.group(1).strip()  
            else:
                return None
                
        #获取帖子一共有多少页
        def getPageNum(self,page):
            pattern = re.compile('<li class="l_reply_num.*?</span>.*?<span.*?>(.*?)</span>',re.S)
            result = re.search(pattern,page)
            if result:
                return result.group(1).strip()
            else:
                return None
                 
        #获取每一层楼的内容,传入页面内容
        def getContent(self,page):
            pattern = re.compile('<div id="post_content_.*?>(.*?)</div>',re.S)
            items = re.findall(pattern,page)
            contents = []
            for item in items:
                #将文本进行去除标签处理,同时在前后加入换行符
                content = "
    "+self.tool.replace(item)+"
    "
                contents.append(content.encode('utf-8'))
                return contents
        
        def setFileTitle(self,title):
            #如果标题不是为None,即成功获取到标题
            if title is not None:
                self.file = open(title + ".txt","w+")
            else:
                self.file = open(self.defaultTitle + ".txt","w+")
        
        def writeData(self,contents):
            #向文件写入每一楼的信息
            for item in contents:
                if self.floorTag == '1':
                    #楼之间的分隔符
                    floorLine = "
    " + str(self.floor) + u"-----------------------------------------------------------------------------------------
    "
                    self.file.write(floorLine)
                self.file.write(item)
                self.floor += 1
            
        def start(self):
            indexPage = self.getPage(1)
            pageNum = self.getPageNum(indexPage)
            title = self.getTitle(indexPage)
            self.setFileTitle(title)
            if pageNum == None:
                print "URL已失效,请重试"
                return
            try:
                print "该帖子共有" + str(pageNum) + ""
                for i in range(1,int(pageNum)+1):
                    print "正在写入第" + str(i) + "页数据"
                    page = self.getPage(i)
                    contents = self.getContent(page)
                    self.writeData(contents)
            #出现写入异常
            except IOError,e:
                print "写入异常,原因" + e.message
            finally:
                print "写入任务完成"
      
    
    print u"请输入帖子代号"
    baseURL = 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))
    seeLZ = raw_input("是否只获取楼主发言,是输入1,否输入0
    ")
    floorTag = raw_input("是否写入楼层信息,是输入1,否输入0
    ")
    bdtb = BDTB(baseURL,seeLZ,floorTag)
    bdtb.start()

  • 相关阅读:
    全文检索 部署及使用
    mysql 数据库常见的一些基本操作 !详不详细你说了算!
    Django 语法笔记
    CentOs Linux 对于 修改 yum源 为 阿里
    小白老凯,初出茅庐!请多关照!简单分享一些 mysql 数据库的安装操作!请给为大神雅正!
    sql server 之一条Sql语句引发的悲剧
    翻译高质量JavaScript代码书写基本要点(转载)
    翻编JavaScript有关的10个怪癖和秘密(转载)
    IIS7.5部署ASP.NET失败
    linq to sql报错,
  • 原文地址:https://www.cnblogs.com/fjl-vxee/p/5765126.html
Copyright © 2011-2022 走看看