Python爬虫(二)爬百度贴吧楼主发言

爬取电影吧一个帖子里的所有楼主发言:

# python2
# -*- coding: utf-8 -*-

import urllib2
import string
import re

class Baidu_Spider:
    feature_pattern = re.compile(r'id="post_content.*?>\s+(.*?)</div>', re.S)
    replaceList = [('&#39;', '\''), ('&quot;', '\"')]

    def __init__(self, url):
        self.url = url + '?see_lz=1'

    def crawl_tieba_lz(self):
        begin_page = urllib2.urlopen(self.url).read()
        self.print_page_title(begin_page)
        count = self.get_page_count(begin_page)
        self.handle_data(count)

    def handle_data(self, count):
        f = open('tieba_lz.txt', 'w+')
        for i in range(count):
            url = self.url + '&pn=' + str(i+1)
            hint = '' + str(i+1) + ''

            print '正在下载%s: %s' % (hint, url)
            page = urllib2.urlopen(url).read()
            features = re.findall(self.feature_pattern, page)
            print hint + '下载完成'
            print '共有%d条记录' % len(features)

            f.write(hint + ':\n')
            for feature in features:
                feature = self.handle_record(feature)
                print feature
                f.write(feature + '\n\n')
        f.close()
        print 'done'

    def handle_record(self, record):
        record = re.sub(r'(<|</)br>', '\n', record)
        record = re.sub(r'<.*?>', '', record)
        for item in self.replaceList:
            record = record.replace(item[0], item[1])
        return record

    def get_page_count(self, page):
        result = re.search(r'class="red">(\d+?)</span>', page, re.S)
        if result:
            count = int(result.group(1))
            print '一共%d页' % count
        else:
            count = 0;
            print '无法获取页数'
        return count

    def print_page_title(self, page):
        result = re.search(r'<h1.*?>(.*?)</h1>', page, re.S)
        if result:
            title = result.group(1)
            print '标题: %s' % title
        else:
            print '无法获取标题'

spider = Baidu_Spider('http://tieba.baidu.com/p/4082863285')
spider.crawl_tieba_lz()

 

转载于:https://www.cnblogs.com/gattaca/p/6739436.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值