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

来源:互联网 发布:淘宝卖衣服需要什么证 编辑:程序博客网 时间:2024/05/21 08:41

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

'''author:superWangdate:2017-09-14re模板:2.2.1requests模板:2.18.4bs4模板:4.6.0爬取糗事百科的笑话'''#!/usr/bin/env python#-*- coding:utf-8 -*-import requestsfrom bs4 import BeautifulSoupimport reclass 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)

效果图:
效果图:

原创粉丝点击