Single

哔哩哔哩弹幕词云图生成

我觉得注释写的很详细,没必要写啥思想过程了

import tkinter as tk
from tkinter import *
import requests
import wordcloud
import bs4

# 这里是请求方法,一次性把请求头加好 免得麻烦
def get_html(url):
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.98 Safari/537.36'}
    res = requests.get(url, headers=headers)

    return res


def get_danmu():
    print(box_bv.get())
    BV = box_bv.get()  # BV号来自用户输入
    # 这里拼装请求地址,API来自Github,偷懒拿了个现成的,https://github.com/socialsisteryi/bilibili-api-collect
    url = 'http://api.bilibili.com/x/web-interface/view?bvid=%s' % BV
    a = get_html(url)

    data = a.json()
    info_ = data.get('data')  # 解析json保存到字典
    cid = info_['pages'][0]['cid']  # 这里是取弹幕组的ID
    print(cid)

    url2 = 'http://api.bilibili.com/x/v1/dm/list.so?oid=%s' % cid  # 这里是取弹幕组的接口
    danmulist = get_html(url2)
    data = danmulist.content.decode('utf-8')
    soup = bs4.BeautifulSoup(data, 'lxml')
    result = soup.find_all('d')
    danmu = ''
    danmudic = {}
    n = 1
    # 下面是弹幕统计
    for i in result:
        a = i.text
        # 判断是否首次
        if a in danmudic:
            n += 1
        else:
            n = 1
        danmudic[a] = n
        danmu += a
        danmu += ','
    # 下面是生成词云图
    wc = wordcloud.WordCloud(font_path=r'C:\Windows\Font\simfang.ttf',
                             background_color='white', height=412, width=472)
    wc.generate(danmu)
   # image = wc.to_image()
    wc.to_file('temp.png')

    # 装填弹幕列表
    danmu_ = sorted(danmudic.items(), key=lambda kv: kv[1], reverse=True)
    for i in danmu_:
        box_danmu.insert(END, i)

    # 加个滚动条吧 提升一下使用体验
    yscrollbar = Scrollbar(box_danmu, command=box_danmu.yview)
    yscrollbar.pack(side=RIGHT, fill=Y)
    box_danmu.config(yscrollcommand=yscrollbar.set)

    # 最后就是显示词云图了
    global photo  # 必须全局
    photo = tk.PhotoImage(file='temp.png')
    Picture1 = tk.Label(root, imag=photo)
    Picture1.place(x=174, y=13, width=472, height=412)


if __name__ == '__main__':
    # 主窗体
    root = tk.Tk()
    window_get = root
    window_get.title('B站弹幕词云图生成器')
    window_get.resizable(width=False, height=False)
    screenwidth = window_get.winfo_screenwidth()
    screenheight = window_get.winfo_screenheight()
    size = '%dx%d+%d+%d' % (656, 436, (screenwidth - 656) /
                            2, (screenheight - 436) / 2)
    window_get.geometry(size)
    #方便测试 写一个默认文本
    default_value = StringVar()
    default_value.set('1C54y197CT')
    # BV输入框
    box_bv = Entry(window_get,textvariable = default_value)
    box_bv.grid(row=0, column=1)
    box_bv.place(x=6, y=76, width=156, height=45)
    # 按钮
    Button_get_title = tk.StringVar()
    Button_get_title.set('获取弹幕&生成词云图')
    Button_get = tk.Button(
        window_get, textvariable=Button_get_title, command=get_danmu)
    Button_get.place(x=7, y=128, width=156, height=45)
    # 提示标签
    Label1_title = tk.StringVar()
    Label1_title.set('请输入BV号')
    Label1 = tk.Label(window_get, textvariable=Label1_title, anchor=tk.W)
    Label1.place(x=8, y=16, width=156, height=45)
    # 弹幕
    box_danmu = Listbox(window_get)
    box_danmu.place(x=6, y=179, width=156, height=251)

    root.mainloop()

下面上测试图

暂无评论

发表评论