爬取糗事百科的笑话,每按一下Enter键,输出一条信息

这是一个简单的爬虫,爬取糗事百科的笑话,每按一下Enter键,输出一条信息。

'''
author:superWang
date:2017-09-14
re模板:2.2.1
requests模板:2.18.4
bs4模板:4.6.0
爬取糗事百科的笑话
'''

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import requests
from bs4 import BeautifulSoup
import re

class GetQiuShiBaiKeInfo():

    #得到url中的信息
    def getPageInfo(self,url,page):
        res = requests.get(url)
        #print(res.text)
        soup = BeautifulSoup(res.text,"html5lib")
        articles = soup.select(".article")
        for article in articles:
            #得到作者信息
            author = article.select(".author")[0].select("h2")[0].text.strip()
            #得到内容
            content = article.select(".content span")[0].text.strip()
            #得到好笑数
            stats_vote = article.select(".stats .stats-vote i")[0].text
            #得到评论数
            stats_comments = article.select(".stats .stats-comments a i")[0].text
            print(author + "\t内容:" + content + "\t好笑:" + stats_vote + "\t评论:" + stats_comments)
            #循环直到按下Enter键时才输出
            while True:
                input1 = input()
                if input1 == '':
                    break
        print("这一页已看完,需要继续看下一页吗?y:是,n:否")
        while True:
            input2 = input()
            if input2 == 'y':
                page = page +1
                self.getInfo(page)
            elif input2 == 'n':
                break
            else:
                pass

    #得到page页的信息
    def getInfo(self,page):
        url = "https://www.qiushibaike.com/hot/page/"+str(page)+"/"
        self.getPageInfo(url,page)


if __name__ == '__main__':
    a = GetQiuShiBaiKeInfo()
    a.getInfo(1)

效果图:
效果图:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值