虎牙数据分析-可视化-爬虫-GUI界面结合

在这个全民直播的时代,我们基本上都观看过直播,我经常浏览的是虎牙直播平台,主要是游戏方面的,因此我使用我学的python数据分析方面的知识,制作了一个对虎牙实时三个游戏的主播和观看人数的数据分析和数据可视化,这个项目总体主要包含了这几个模块:

github:https://github.com/Spider-Man123/Data-analysis-of-Tiger-teeth-live-broadcast/tree/master

1.爬虫模块

对虎牙的英雄联盟,绝地求生,穿越火线这三个板块的主播和相对应的观看人数进行数据爬取

2.数据分析模块

对得到的数据进行排序,统计,然后使用条形图对英雄联盟和穿越火线这两个板块的观看范围的人数进行显示,使用扇形图对这三个游戏的总管看人数进行比较,使用条形图对这三个游戏的观看人数排行榜前十进行显示

3.GUI界面模块

编写一个可视化界面,达到使用便捷的目的

代码如下:

from tkinter import *
from pylab import *
import operator
import requests
#爬虫模块1:对数据进行统计返回排行版前十的数据
def pachong2(url):
    n=[]
    list1=[]
    for i in range(1,4):
        url = url[0:-1] + '{}'.format(i)
        r=requests .get(url).json()
        for j in r['data']['datas']:
            data = {
                '主播': j['nick'],
                '人数':float("%.2f" %(float(j['totalCount'])/10000)),
            }
            list1.append(data)
    sorted_x = sorted(list1, key=operator.itemgetter('人数'))
    for k in range(0,len(sorted_x )):
        if k>=len(sorted_x )-10:
            n.append(sorted_x[k])
    return n
#使用条形图对三个游戏的排行榜前十进行显示
def hit_me3():
    window.destroy()
    root = Tk()
    root.geometry('3000x3000')
    root.title('spider-man')
    l = Label(root, text='\n' + '\n' + '                 请选择游戏', font=('宋体', 50),
              foreground='blue', underline=1, anchor='nw'
              , width=100, height=5, bg='orange')
    l.pack()
    #英雄联盟
    def pa1():
        root.destroy()
        list1 = pachong2('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1&tagAll=0&page=2')
        m = []
        n = []
        for i in range(0, len(list1)):
            m.append(list1[i]['主播'])
            n.append(list1[i]['人数'])
        mpl.rcParams['font.sans-serif'] = ['SimHei']
        patches = plt.bar(x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], height=n, width=0.5, color='k',
                          tick_label=m)
        plt.legend()
        plt.ylabel('人数/万')
        plt.xlabel('主播')
        plt.title('英雄联盟排行榜前十统计图')
        for rect in patches:
            height = rect.get_height()
            if height != 0:
                plt.text(rect.get_x() + rect.get_width() / 10, height + 10, '{}'.format(height))
        plt.show()
    #穿越火线
    def pa2():
        root.destroy()
        list1 = pachong2('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=4&tagAll=0&page=2')
        m = []
        n = []
        for i in range(0, len(list1)):
            m.append(list1[i]['主播'])
            n.append(list1[i]['人数'])
        mpl.rcParams['font.sans-serif'] = ['SimHei']
        patches = plt.bar(x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], height=n, width=0.5, color='k',
                          tick_label=m)
        plt.legend()
        plt.ylabel('人数/万')
        plt.xlabel('主播')
        plt.title('穿越火线排行榜前十统计图')
        for rect in patches:
            height = rect.get_height()
            if height != 0:
                plt.text(rect.get_x() + rect.get_width() / 10, height + 10, '{}'.format(height))
        plt.show()
    #绝地求生
    def pa3():
        root .destroy()
        list1 = pachong2('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2793&tagAll=0&page=2')
        m = []
        n = []
        for i in range(0, len(list1)):
            m.append(list1[i]['主播'])
            n.append(list1[i]['人数'])
        mpl.rcParams['font.sans-serif'] = ['SimHei']
        patches = plt.bar(x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], height=n, width=0.5, color='k',
                          tick_label=m)
        plt.legend()
        plt.ylabel('人数/万')
        plt.xlabel('主播')
        plt.title('绝地求生排行榜前十统计图')
        for rect in patches:
            height = rect.get_height()
            if height != 0:
                plt.text(rect.get_x() + rect.get_width() / 10, height + 10, '{}'.format(height))
        plt.show()
    r1 = Radiobutton(root, text='英雄联盟', font=('宋体', 30), bg='orange', command=pa1)
    r1.pack()
    r2 = Radiobutton(root, text='穿越火线', font=('宋体', 30), bg='orange', command=pa2)
    r2.pack()
    r3 = Radiobutton(root, text='绝地求生', font=('宋体', 30), bg='orange', command=pa3)
    r3.pack()
    root.mainloop()
#使用扇形图对总人数进行对比
def hit_me2():
    window .destroy()
    list1=pachong('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1&tagAll=0&page=2')
    list2=pachong('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=4&tagAll=0&page=2')
    list3=pachong('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=2793&tagAll=0&page=2')
    m=0
    n=0
    k=0
    for i in list1:
        m=m+i['人数']
    for i in list2:
        n=n+i['人数']
    for i in list3:
        k=k+i['人数']
    mpl.rcParams['font.sans-serif'] = ['SimHei']
    plt.figure(figsize=(6, 9))
    labels = ['英雄联盟', '穿越火线', '绝地求生']
    sizes = [m,n,k]
    colors = ['red', 'yellowgreen', 'lightskyblue']
    explode = (0.05, 0, 0)

    patches, l_text, p_text = plt.pie(sizes, explode=explode, labels=labels, colors=colors,
                                      labeldistance=1.1, autopct='%3.1f%%', shadow=False,
                                      startangle=90, pctdistance=0.6)
    for t in l_text:
        t.set_size = (30)
    for t in p_text:
        t.set_size = (20)
    plt.axis('equal')
    plt.legend()
    plt.show()
#爬虫模块2:爬取主播和相对应的观看人数,然后进行排序返回
def pachong(url):
    list1=[]
    for i in range(1,4):
        url = url[0:-1] + '{}'.format(i)
        r=requests .get(url).json()
        for j in r['data']['datas']:
            data = {
                '主播': j['nick'],
                '人数':float("%.2f" %(float(j['totalCount'])/10000)),
            }
            list1.append(data)
    sorted_x = sorted(list1, key=operator.itemgetter('人数'))
    return sorted_x
window = Tk()
window.geometry('3000x3000')
window.title('spider-man')
l = Label(window, text='\n'+'                欢迎使用虎牙直播数据分析器', font=('宋体', 40),
          foreground='blue',underline=1, anchor='nw'
          ,width=100, height=3, bg='orange')
l.pack()
def hit_me():
    window.destroy()
    list20 = pachong('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1&tagAll=0&page=2')
    list21=pachong('https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=4&tagAll=0&page=2')
    def tongji(list20):
        list1 = []
        list2 = []
        list3 = []
        list4 = []
        list5 = []
        list6 = []
        list7 = []
        list8 = []
        list9 = []
        list10 = []
        list11 = []
        list12 = []
        list13 = []
        list14 = []
        for i in range(0, len(list20)):
            list1.append(list20[i]['主播'])
            list2.append(list20[i]['人数'])
        for i in range(0, len(list2)):
            if list2[i] >= 0 and list2[i] <= 10:
                list3.append(list20[i]['主播'])
            if list2[i] > 10 and list2[i] <= 20:
                list4.append(list20[i]['主播'])
            if list2[i] > 20 and list2[i] <= 30:
                list5.append(list20[i]['主播'])
            if list2[i] > 30 and list2[i] <= 40:
                list6.append(list20[i]['主播'])
            if list2[i] > 40 and list2[i] <= 50:
                list7.append(list20[i]['主播'])
            if list2[i] > 50 and list2[i] <= 60:
                list8.append(list20[i]['主播'])
            if list2[i] > 60 and list2[i] <= 70:
                list9.append(list20[i]['主播'])
            if list2[i] > 70 and list2[i] <= 80:
                list10.append(list20[i]['主播'])
            if list2[i] > 80 and list2[i] <= 90:
                list11.append(list20[i]['主播'])
            if list2[i] > 90 and list2[i] <= 100:
                list12.append(list20[i]['主播'])
            if list2[i] > 100 and list2[i] <= 200:
                list13.append(list20[i]['主播'])
            if list2[i] > 200:
                list14.append(list20[i]['主播'])
        list15 = [len(list3), len(list4), len(list5), len(list6), len(list7), len(list8), len(list9), len(list10),
                  len(list11), len(list12), len(list13), len(list14)]
        return list15
    #使用条形图对英雄联盟和穿越火线的观看人数的范围就行人数统计

    def tiao(list1, list2):
        mpl.rcParams['font.sans-serif'] = ['SimHei']
        patches = plt.bar(x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], height=list1, width=0.5, color='k',
                          tick_label=['0-10', '10-20', '20-30', '30-40', '40-50', '50-60', '60-70', '70-80', '80-90',
                                      '90-100', '100-200', '>200'], label='英雄联盟')
        patches2 = plt.bar(x=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], height=list2, width=0.3, color='r',
                           tick_label=['0-10', '10-20', '20-30', '30-40', '40-50', '50-60', '60-70', '70-80', '80-90',
                                       '90-100', '100-200', '>200'], label='穿越火线')
        plt.legend()
        plt.ylabel('人数/个')
        plt.xlabel('范围/万')
        plt.title('虎牙人数统计图')
        for rect in patches:
            height = rect.get_height()
            if height != 0:
                plt.text(rect.get_x() + rect.get_width() / 10, height + 10, '{}'.format(height))
        for rect in patches2:
            height = rect.get_height()
            if height != 0:
                plt.text(rect.get_x() + rect.get_width() / 10, height + 5, '{}'.format(height), color='r')
        plt.show()
    if __name__ == '__main__':
        list30 = tongji(list20)
        list31 = tongji(list21)
        tiao(list30, list31)
#GUI界面
r1= Radiobutton(window,text='人气范围统计图',font=('宋体',40),bg='orange',command=hit_me)
r1.pack()
r2 = Radiobutton(window,text='全部人气比较图',font=('宋体',40),bg='orange',command=hit_me2)
r2.pack()
r3 = Radiobutton(window,text='游戏排行榜人气前十',font=('宋体',40),bg='orange',command=hit_me3)
r3.pack()
canvas=Canvas(window,bg='blue',height=450,width=450)
image_file=PhotoImage (file='2.png')
canvas .create_image(0,0,anchor='nw',image=image_file)
canvas .pack()
window .mainloop()

运行效果:

开始界面

在这里插入图片描述
人气范围统计图
在这里插入图片描述
全部人气比较图
在这里插入图片描述
游戏排行版前十界面
在这里插入图片描述
英雄联盟
在这里插入图片描述

  • 7
    点赞
  • 35
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值