爬取斗鱼直播并分析数据

  • Post author:
  • Post category:其他


"""
auth:Brussels
"""
import requests
import time


class DouYu(object):

    def __init__(self):
        self.url = 'https://www.douyu.com/gapi/rkc/directory/0_0/'
        self.page = 1
        self.num = 1
        self.artist_info = {}
        self.total_hot = 0
        self.flag = 1

    def __get_response(self):  # 请求接口,返回响应
        res = requests.get(self.url+str(self.page))
        response = res.json()
        return response

    def __get_first_name(self):  # 拿到第一页第一个的主播姓名,用来判断循环重复
        url = 'https://www.douyu.com/gapi/rkc/directory/0_0/1'
        res = requests.get(url)
        response = res.json()
        first_name = response['data']['rl'][0]['nn']
        return first_name

    def get_artist_info(self):
        first_name = self.__get_first_name()  # 得到当前排名第一个主播姓名,用来判断循环结束

        while DouYu.flag == 1:
            response = self.__get_response()
            self.info = response['data']['rl']
            for i in range(len(self.info)):
                self.info = response['data']['rl']
                name = self.info[i]['nn']
                game = self.info[i]['c2name']
                hot = int(self.info[i]['ol'])
                if self.num > 300:
                    if first_name == name:
                        self.flag = 0
                        break
                game_and_hot = []
                game_and_hot.append(game)
                game_and_hot.append(hot)
                self.artist_info[name] = game_and_hot
                self.num += 1  # 计算直播数量
                print("{}.主播姓名:{}, 人气:{:,},直播类型:{}".format(self.num, name, hot, game))
            DouYu.page += 1
        return self.artist_info

    def analyze_info(self, artist_info):
        game_num = {}
        info = sorted(artist_info.items(), key=lambda x: x[1][1], reverse=True)
        print("\n==统计时间为:{}".format(time.strftime("%Y-%m-%d %X", time.localtime())))
        print("当前直播人数:{:,}".format(self.num))
        print("当前斗鱼人气排名前十的主播是:")
        for i in range(10):
            print("{},【{}】 - 人气热度:{:,} - 【{}】".format(i+1, info[i][0], info[i][1][1], info[i][1][0]))
        game_list = {}
        for l in info:
            game = l[1][0]
            hot = l[1][1]
            self.total_hot += hot  # 统计总热度
            game_list[game] = game_list.get(game, 0) + hot
            game_num[game] = game_num.get(game, 0) + 1
        game_num = sorted(game_num.items(), key=lambda x: x[1], reverse=True)
        game_list = sorted(game_list.items(), key=lambda x: x[1], reverse=True)
        print("\n当前斗鱼直播类型热度排名前十:")
        for k in range(10):
            game_name = game_list[k][0]
            game_hot = game_list[k][1]
            print("{},【{}】 - 人气热度:{} - 占比:{:.2f}%".format(k+1, game_name, game_hot, (game_hot/self.total_hot)*100))
        print("\n当前斗鱼最多人在直播的类型前十:")
        for p in range(10):
            game_name = game_num[p][0]
            game_number = game_num[p][1]
            print("{},当前共有{}人正在直播【{}】 - 占比:{:.2f}%".format(p+1, game_number, game_name, (game_number/self.num)*100))


if __name__ == '__main__':
    DouYu = DouYu()
    DouYu.analyze_info(DouYu.get_artist_info())

输出示例如下

==统计时间为:2019-10-06 19:56:22
当前直播人数:24,093
当前斗鱼人气排名前十的主播是:
1,【旭旭宝宝】 - 人气热度:5,872,297 - 【DNF】
2,【王者荣耀官方赛事】 - 人气热度:5,368,874 - 【王者荣耀】
3,【即将拥有人鱼线的PDD】 - 人气热度:4,828,741 - 【英雄联盟】
4,【一条小团团OvO】 - 人气热度:3,709,850 - 【绝地求生】
5,【骚白】 - 人气热度:2,443,191 - 【王者荣耀】
6,【穿越火线运营团队】 - 人气热度:2,373,043 - 【穿越火线】
7,【王者荣耀韩国官方赛事】 - 人气热度:2,077,951 - 【王者荣耀】
8,【呆妹儿小霸王】 - 人气热度:1,963,947 - 【绝地求生】
9,【雨神丶】 - 人气热度:1,902,938 - 【传奇】
10,【腾讯活动专用直播间】 - 人气热度:1,898,204 - 【天涯明月刀】

当前斗鱼直播类型热度排名前十:
1,【王者荣耀】 - 人气热度:36090052 - 占比:10.79%
2,【绝地求生】 - 人气热度:34961778 - 占比:10.46%
3,【英雄联盟】 - 人气热度:29798949 - 占比:8.91%
4,【和平精英】 - 人气热度:18880968 - 占比:5.65%
5,【DNF】 - 人气热度:16208454 - 占比:4.85%
6,【颜值】 - 人气热度:13509162 - 占比:4.04%
7,【户外】 - 人气热度:13161212 - 占比:3.94%
8,【一起看】 - 人气热度:11343427 - 占比:3.39%
9,【lol云顶之弈】 - 人气热度:9150507 - 占比:2.74%
10,【魔兽怀旧服】 - 人气热度:8608879 - 占比:2.57%

当前斗鱼最多人在直播的类型前十:
1,当前共有2916人正在直播【王者荣耀】 - 占比:12.10%
2,当前共有2748人正在直播【英雄联盟】 - 占比:11.41%
3,当前共有1811人正在直播【绝地求生】 - 占比:7.52%
4,当前共有1291人正在直播【魔兽怀旧服】 - 占比:5.36%
5,当前共有995人正在直播【lol云顶之弈】 - 占比:4.13%
6,当前共有968人正在直播【和平精英】 - 占比:4.02%
7,当前共有836人正在直播【颜值】 - 占比:3.47%
8,当前共有643人正在直播【一起看】 - 占比:2.67%
9,当前共有597人正在直播【魔兽世界】 - 占比:2.48%
10,当前共有573人正在直播【DNF】 - 占比:2.38%



版权声明:本文为weixin_42852210原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。