zoukankan      html  css  js  c++  java
  • python爬虫实战(2)--爬取百度贴吧

    本篇目标

    1.对百度贴吧的任意帖子进行抓取

    2.指定是否只抓取楼主发帖内容

    3.将抓取到的内容分析并保存到文件

    1.URL格式的确定

    先观察百度贴吧url格式,以中南财经政法大学迎新帖为例,URL我们划分基础部分是 http://tieba.baidu.com/p/5174106966,参数部分是 ?see_lz=1&pn=1

    2.页面的抓取

    一个初始化方法,一个获取页面的方法。另外,获取页面的方法我们需要知道一个参数就是帖子页码,所以这个参数的指定我们放在该方法中。

    class Baidu_Spider:
        # 申明相关的属性
        def __init__(self, url):
            self.myUrl = url + '?see_lz=1'
            self.datas = []
            self.myTool = HTML_Tool()
            self.floor = 1
            print u'已经启动百度贴吧爬虫'
    
            # 初始化加载页面并将其转码储存
    
        def baidu_tieba(self):
            # 读取页面的原始信息并将其从utf-8转码
            myPage = urllib2.urlopen(self.myUrl).read().decode('utf-8')
            # 计算楼主发布内容一共有多少页
            endPage = self.page_counter(myPage)
            # 获取该帖的标题
            title = self.find_title(myPage)
            print u'文章名称:' + title
            # 获取最终的数据
            self.save_data(self.myUrl, title, endPage)
    
    # 以中南财经政法大学迎新帖为例子
    # bdurl = 'http://tieba.baidu.com/p/5174106966?see_lz=1&pn=1'
    
    print u'请输入贴吧的地址最后的数字串:'
    bdurl = 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))
    初始代码

    3.提取相关信息

    1)提取帖子页数

            # 用来计算一共有多少页
    
        def page_counter(self, myPage):
            # 匹配 "共有<span class="red">12</span>页" 来获取一共有多少页
            myMatch = re.search(r'class="red">(d+?)</span>', myPage, re.S)
            if myMatch:
                endPage = int(myMatch.group(1))
                print u'爬虫报告:发现楼主共有%d页的原创内容' % endPage
            else:
                endPage = 0
                print u'爬虫报告:无法计算楼主发布内容有多少页!'
            return endPage
    提取页数

    2)提取帖子标题

            # 用来寻找该帖的标题
    
        def find_title(self, myPage):
            # 匹配 <h1 class="core_title_txt" title="">xxxxxxxxxx</h1> 找出标题
            myMatch = re.search(r'<h1.*?>(.*?)</h1>', myPage, re.S)
            title = u'暂无标题'
            if myMatch:
                title = myMatch.group(1)
            else:
                print u'爬虫报告:无法加载文章标题!'
                # 文件名不能包含以下字符:  / : * ? " < > |
            title = title.replace('\', '').replace('/', '').replace(':', '').replace('*', '').replace('?', '').replace('"',
                                                                                                                        '').replace(
                '>', '').replace('<', '').replace('|', '')
            return title
    提取标题

    3)获取信息并保存

            # 用来存储楼主发布的内容
    
        def save_data(self, url, title, endPage):
            # 加载页面数据到数组中
            self.get_data(url, endPage)
            # 打开本地文件
            f = open(title + '.txt', 'w+')
            f.writelines(self.datas)
            f.close()
            print u'爬虫报告:文件已下载到本地并打包成txt文件'
            print u'请按任意键退出...'
            raw_input();
    
            # 获取页面源码并将其存储到数组中
    
        def get_data(self, url, endPage):
            url = url + '&pn='
            for i in range(1, endPage + 1):
                print u'爬虫报告:爬虫%d号正在加载中...' % i
                myPage = urllib2.urlopen(url + str(i)).read().decode('utf-8')
                # 将myPage中的html代码处理并存储到datas里面
                self.deal_data(myPage)
    
    
                # 将内容从页面代码中抠出来
    
        def deal_data(self, myPage):
            myItems = re.findall('id="post_content.*?>(.*?)</div>', myPage, re.S)
            for item in myItems:
                data = self.myTool.Replace_Char(item.encode('gbk'))
                myFloor = (str(self.floor) + "楼--------------------------------------").decode('utf-8').encode('gbk')
                self.datas.append(myFloor + '
    
    ' + data + '
    
    ')
                self.floor += 1
    提取信息

    4.完善代码

    # -*- coding: utf-8 -*-
    # ---------------------------------------
    #   程序:百度贴吧爬虫
    #   作者:lzhc
    #   日期:2017-12-11
    #   语言:Python 2.7
    #   操作:输入网址后自动只看楼主并保存到本地文件
    #   功能:将楼主发布的内容打包txt存储到本地。
    # ---------------------------------------
    
    import string
    import urllib2
    import re
    
    
    # ----------- 处理页面上的各种标签 -----------
    class HTML_Tool:
        # 用非 贪婪模式 匹配 	 或者 
     或者 空格 或者 超链接 或者 图片
        BgnCharToNoneRex = re.compile("(	|
    | |<a.*?>|<img.*?>)")
    
        # 用非 贪婪模式 匹配 任意<>标签
        EndCharToNoneRex = re.compile("<.*?>")
    
        # 用非 贪婪模式 匹配 任意<p>标签
        BgnPartRex = re.compile("<p.*?>")
        CharToNewLineRex = re.compile("(<br>|</p>|<tr>|<div>|</div>)")
        CharToNextTabRex = re.compile("<td>")
    
        def Replace_Char(self, x):
            x = self.BgnCharToNoneRex.sub("", x)
            x = self.BgnPartRex.sub("
        ", x)
            x = self.CharToNewLineRex.sub("
    ", x)
            x = self.CharToNextTabRex.sub("	", x)
            x = self.EndCharToNoneRex.sub("", x)
            return x
    
    
    class Baidu_Spider:
        # 申明相关的属性
        def __init__(self, url):
            self.myUrl = url + '?see_lz=1'
            self.datas = []
            self.myTool = HTML_Tool()
            self.floor = 1
            print u'已经启动百度贴吧爬虫'
    
            # 初始化加载页面并将其转码储存
    
        def baidu_tieba(self):
            # 读取页面的原始信息并将其从utf-8转码
            myPage = urllib2.urlopen(self.myUrl).read().decode('utf-8')
            # 计算楼主发布内容一共有多少页
            endPage = self.page_counter(myPage)
            # 获取该帖的标题
            title = self.find_title(myPage)
            print u'文章名称:' + title
            # 获取最终的数据
            self.save_data(self.myUrl, title, endPage)
    
            # 用来计算一共有多少页
    
        def page_counter(self, myPage):
            # 匹配 "共有<span class="red">12</span>页" 来获取一共有多少页
            myMatch = re.search(r'class="red">(d+?)</span>', myPage, re.S)
            if myMatch:
                endPage = int(myMatch.group(1))
                print u'爬虫报告:发现楼主共有%d页的原创内容' % endPage
            else:
                endPage = 0
                print u'爬虫报告:无法计算楼主发布内容有多少页!'
            return endPage
    
            # 用来寻找该帖的标题
    
        def find_title(self, myPage):
            # 匹配 <h1 class="core_title_txt" title="">xxxxxxxxxx</h1> 找出标题
            myMatch = re.search(r'<h1.*?>(.*?)</h1>', myPage, re.S)
            title = u'暂无标题'
            if myMatch:
                title = myMatch.group(1)
            else:
                print u'爬虫报告:无法加载文章标题!'
                # 文件名不能包含以下字符:  / : * ? " < > |
            title = title.replace('\', '').replace('/', '').replace(':', '').replace('*', '').replace('?', '').replace('"',
                                                                                                                        '').replace(
                '>', '').replace('<', '').replace('|', '')
            return title
    
    
            # 用来存储楼主发布的内容
    
        def save_data(self, url, title, endPage):
            # 加载页面数据到数组中
            self.get_data(url, endPage)
            # 打开本地文件
            f = open(title + '.txt', 'w+')
            f.writelines(self.datas)
            f.close()
            print u'爬虫报告:文件已下载到本地并打包成txt文件'
            print u'请按任意键退出...'
            raw_input();
    
            # 获取页面源码并将其存储到数组中
    
        def get_data(self, url, endPage):
            url = url + '&pn='
            for i in range(1, endPage + 1):
                print u'爬虫报告:爬虫%d号正在加载中...' % i
                myPage = urllib2.urlopen(url + str(i)).read().decode('utf-8')
                # 将myPage中的html代码处理并存储到datas里面
                self.deal_data(myPage)
    
    
                # 将内容从页面代码中抠出来
    
        def deal_data(self, myPage):
            myItems = re.findall('id="post_content.*?>(.*?)</div>', myPage, re.S)
            for item in myItems:
                data = self.myTool.Replace_Char(item.encode('gbk'))
                myFloor = (str(self.floor) + "楼--------------------------------------").decode('utf-8').encode('gbk')
                self.datas.append(myFloor + '
    
    ' + data + '
    
    ')
                self.floor += 1
    
    
    
                # -------- 程序入口处 ------------------
    
    
    print u"""#--------------------------------------- 
    #   程序:百度贴吧爬虫  
    #   作者:lzhc 
    #   日期:2017-12-11
    #   语言:Python 2.7 
    #   操作:输入网址后自动只看楼主并保存到本地文件 
    #   功能:将楼主发布的内容打包txt存储到本地。 
    #--------------------------------------- 
    """
    
    # 以中南财经政法大学迎新帖为例子
    # bdurl = 'http://tieba.baidu.com/p/5174106966?see_lz=1&pn=1'
    
    print u'请输入贴吧的地址最后的数字串:'
    bdurl = 'http://tieba.baidu.com/p/' + str(raw_input(u'http://tieba.baidu.com/p/'))
    
    # 调用
    mySpider = Baidu_Spider(bdurl)
    mySpider.baidu_tieba()

    运行结果:

    记入记事本中的内容:

  • 相关阅读:
    vue使用watch 观察路由变化,重新获取内容
    Intellij IDEA 最新旗舰版注册激活破解(2018亲测,可用)
    前端开发浏览器兼容问题
    基于Docker搭建MySQL主从复制
    js获取计算后的样式表
    js对象取值的两种方式
    js中的style与jQuery中的css
    js页面加载函数
    代码中jndi数据源的支持
    Oracle中的sid与servicename
  • 原文地址:https://www.cnblogs.com/lzhc/p/8024962.html
Copyright © 2011-2022 走看看