获取糗事百科段子内容(根据网络代码改编)

本文分享了一个糗事百科段子爬虫的实战案例,针对糗事百科版本更新导致原代码无法运行的问题,作者对代码进行了修改并分享了完整的代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近在学习获取网络数据,在网上看到一个获取获取糗事百科段子内容的实例(点击打开链接

因为糗事百科的版本变化了所以原代码不可以直接使用,在评论中有可直接使用的代码,下面的代码是我根据博主的源码改编而成的,思路和方法可以看博主原文

# encoding: utf-8
import urllib2
import re

class Qsbk():

    def __init__(self):
        self.url = 'http://www.qiushibaike.com/hot/page/'
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        # 初始化headers
        self.headers = {'User-Agent': self.user_agent}

    def html_data(self, pageIndex):
        try:
            url = self.url + str(pageIndex)
            # 构建请求的request
            request = urllib2.Request(url, headers=self.headers)
            # 利用urlopen获取页面代码
            response = urllib2.urlopen(request)
            # 将页面转化为UTF-8编码
            pageCode = response.read().decode('utf-8')
            # print pageCode
            return pageCode
        except urllib2.URLError, e:
            if hasattr(e, "reason"):
                print u"连接糗事百科失败,错误原因", e.reason
                return None

    def data_split(self, pageIndex):
        pageCode = self.html_data(pageIndex)
        if not pageCode:
            print "页面加载失败...."
            return None
        pattern = re.compile('<div class="author clearfix">.*?<h2>(.*?)</h2>.*?</div>.*?'
                             '<div class="content">(.*?)</div>.*?'
                             '<i class="number">(.*?)</i>.*?<i class="number">(.*?)</i>', re.S)
        items = pattern.findall(pageCode)
        return list(items)

    def out_put(self):
        i = 1
        j = 0
        output = ''
        datats = self.data_split(i)
        while (j < len(datats)):

            print "正在读取糗事百科,按回车查看新段子,q退出"
            output = raw_input()
            if output == '':
                print '第' + str(i) + '页'
                print ''.join(datats[j])
                j += 1
                if(j == len(datats)):
                    i += 1
                    j = 0
                    datats = self.data_split(i)

            elif output == 'q':
                break
            else:
                print "正在读取糗事百科,按回车查看新段子,Q退出"
                output = raw_input()

if __name__ == '__main__':
    Qsbk().out_put()


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值