Python分析bilibili视频弹幕并获取发表最多的前十条弹幕-改进版本

比上一版本增加了一点点功能、返回功能太麻烦了没做

all_ui.py文件

  1 from danmuxinxi_ui import *
  2 from ciyun_ui import *
  3 from video_info_ui import *
  4 from video_fengmian_ui import *
  5 
  6 
  7 class IndexWindow(object):
  8 
  9     def setupUi(self, MainWindow):
 10         MainWindow.setObjectName("MainWindow")
 11         MainWindow.resize(599, 672)
 12         self.centralwidget = QtWidgets.QWidget(MainWindow)
 13         self.centralwidget.setObjectName("centralwidget")
 14         self.label = QtWidgets.QLabel(self.centralwidget)
 15         self.label.setGeometry(QtCore.QRect(70, 30, 471, 61))
 16         self.label.setStyleSheet("background-color: rgb(170, 85, 0);\n"
 17                                  "font: 24pt \"华文行楷\";")
 18         self.label.setObjectName("label")
 19         self.pushButton = QtWidgets.QPushButton(self.centralwidget)
 20         self.pushButton.setGeometry(QtCore.QRect(70, 210, 191, 91))
 21         self.pushButton.setStyleSheet(
 22             "background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
 23             "color: rgb(85, 170, 255);\n"
 24             "font: 75 18pt \"Agency FB\";\n"
 25             "font: 18pt \"Agency FB\";")
 26         self.pushButton.setObjectName("pushButton")
 27         self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
 28         self.pushButton_3.setGeometry(QtCore.QRect(350, 210, 191, 91))
 29         self.pushButton_3.setStyleSheet(
 30             "background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
 31             "color: rgb(85, 170, 255);\n"
 32             "font: 75 18pt \"Agency FB\";\n"
 33             "font: 18pt \"Agency FB\";")
 34         self.pushButton_3.setObjectName("pushButton_3")
 35         self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
 36         self.pushButton_2.setGeometry(QtCore.QRect(70, 370, 191, 91))
 37         self.pushButton_2.setStyleSheet(
 38             "background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
 39             "color: rgb(85, 170, 255);\n"
 40             "font: 75 18pt \"Agency FB\";\n"
 41             "font: 18pt \"Agency FB\";")
 42         self.pushButton_2.setObjectName("pushButton_2")
 43         self.pushButton_4 = QtWidgets.QPushButton(self.centralwidget)
 44         self.pushButton_4.setGeometry(QtCore.QRect(350, 370, 191, 91))
 45         self.pushButton_4.setStyleSheet(
 46             "background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
 47             "color: rgb(85, 170, 255);\n"
 48             "font: 75 18pt \"Agency FB\";\n"
 49             "font: 18pt \"Agency FB\";")
 50         self.pushButton_4.setObjectName("pushButton_4")
 51         self.label_2 = QtWidgets.QLabel(self.centralwidget)
 52         self.label_2.setGeometry(QtCore.QRect(20, 610, 441, 31))
 53         self.label_2.setObjectName("label_2")
 54         MainWindow.setCentralWidget(self.centralwidget)
 55         self.statusbar = QtWidgets.QStatusBar(MainWindow)
 56         self.statusbar.setObjectName("statusbar")
 57         MainWindow.setStatusBar(self.statusbar)
 58 
 59         # 自定义单击事件
 60         self.pushButton.clicked.connect(self.get_danmu)  # 弹幕信息爬取
 61         self.pushButton_3.clicked.connect(self.get_video_info)  # 视频信息爬取
 62         self.pushButton_2.clicked.connect(self.get_ciyun)  # 弹幕词云生成
 63         self.pushButton_4.clicked.connect(self.get_videoimage)  # 视频封面获取
 64 
 65         self.retranslateUi(MainWindow)
 66         QtCore.QMetaObject.connectSlotsByName(MainWindow)
 67 
 68     # 自定义函数供单击事件调用
 69     def get_danmu(self):
 70         ui_danmu.show()
 71         MainWindow.close()
 72 
 73     def get_video_info(self):
 74         ui_video_info.show()
 75         MainWindow.close()
 76 
 77     def get_ciyun(self):
 78         ui_ciyun.show()
 79         MainWindow.close()
 80 
 81     def get_videoimage(self):
 82         ui_video_image.show()
 83         MainWindow.close()
 84 
 85     def retranslateUi(self, MainWindow):
 86         _translate = QtCore.QCoreApplication.translate
 87         MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
 88         self.label.setText(_translate("MainWindow",
 89                                       "<html><head/><body><p align=\"center\"><span style=\" font-weight:600; color:#00aaff;\">哔哩哔哩</span><span style=\" font-weight:600; color:#ffaa00;\">-信息爬取工具</span></p></body></html>"))
 90         self.pushButton.setText(_translate("MainWindow", "弹幕信息爬取"))
 91         self.pushButton_3.setText(_translate("MainWindow", "视频信息爬取"))
 92         self.pushButton_2.setText(_translate("MainWindow", "弹幕词云图生成"))
 93         self.pushButton_4.setText(_translate("MainWindow", "视频封面获取"))
 94         self.label_2.setText(_translate("MainWindow", "代码已开源在个人blog:https://www.cnblogs.com/Taylor162"))
 95 
 96 
 97 if __name__ == "__main__":
 98     import sys
 99 
100     app = QtWidgets.QApplication(sys.argv)
101     MainWindow = QtWidgets.QMainWindow()
102     ui = IndexWindow()
103     ui_danmu = DanMu()
104     ui_video_info = VideoInfo()
105     ui_ciyun = CiYun()
106     ui_video_image = FengMian()
107     ui.setupUi(MainWindow)
108     MainWindow.show()
109     sys.exit(app.exec_())

ciyun_ui.py文件

  1 import re
  2 
  3 import requests
  4 from PyQt5 import QtCore, QtGui, QtWidgets
  5 from matplotlib import pyplot as plt
  6 from wordcloud import WordCloud
  7 
  8 
  9 class CiYun(QtWidgets.QMainWindow):
 10     def __init__(self):
 11         super(CiYun, self).__init__()
 12         self.setupUi(self)
 13         self.retranslateUi(self)
 14 
 15     def setupUi(self, Form):
 16         Form.setObjectName("Form")
 17         Form.resize(599, 672)
 18         self.label = QtWidgets.QLabel(Form)
 19         self.label.setGeometry(QtCore.QRect(10, 10, 251, 61))
 20         self.label.setStyleSheet("background-color: rgb(85, 0, 0);")
 21         self.label.setObjectName("label")
 22         self.label_3 = QtWidgets.QLabel(Form)
 23         self.label_3.setGeometry(QtCore.QRect(10, 84, 121, 21))
 24         self.label_3.setObjectName("label_3")
 25         self.label_2 = QtWidgets.QLabel(Form)
 26         self.label_2.setGeometry(QtCore.QRect(10, 150, 261, 41))
 27         self.label_2.setStyleSheet("background-color: rgb(142, 229, 255);")
 28         self.label_2.setObjectName("label_2")
 29         self.lineEdit = QtWidgets.QLineEdit(Form)
 30         self.lineEdit.setGeometry(QtCore.QRect(10, 110, 401, 31))
 31         self.lineEdit.setObjectName("lineEdit")
 32         self.pushButton_2 = QtWidgets.QPushButton(Form)
 33         self.pushButton_2.setGeometry(QtCore.QRect(420, 110, 91, 31))
 34         self.pushButton_2.setObjectName("pushButton_2")
 35         self.textBrowser = QtWidgets.QTextBrowser(Form)
 36         self.textBrowser.setGeometry(QtCore.QRect(10, 200, 561, 381))
 37         self.textBrowser.setObjectName("textBrowser")
 38 
 39         # 自定义开始
 40         self.pushButton_2.clicked.connect(self.create_ciyun)
 41 
 42         self.retranslateUi(Form)
 43         QtCore.QMetaObject.connectSlotsByName(Form)
 44 
 45     def __getXMlUrl(self, reponse_text):
 46         match_rule = r'cid=(.*?)&aid'
 47         oid = re.search(match_rule, reponse_text).group().replace('cid=', '').replace('&aid', '')
 48         # 通过该 `oid` 参数获得xml的链接
 49         xml_url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=' + oid
 50         return xml_url
 51 
 52     def create_ciyun(self):
 53         bv_url = self.lineEdit.text()
 54         headers = {
 55             "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"}
 56         resp = requests.get(bv_url, headers=headers)
 57         getXMlUrlFun = self.__getXMlUrl
 58         danmu_url = getXMlUrlFun(resp.text)
 59 
 60         danmu = requests.get(url=danmu_url, headers=headers).content.decode()
 61         # print(danmu)
 62         danmu_list = re.findall('<d p=".*?">(.*?)</d>', danmu)
 63         print(danmu_list)
 64         ciyun_text = ("".join(danmu_list))
 65         print(ciyun_text)
 66         # 词云绘制开始
 67         wordcloud = WordCloud(font_path="simfang.ttf", height=370, width=550).generate(ciyun_text)
 68         wordcloud.to_file('bilibiili_词云.png')
 69         plt.imshow(wordcloud, interpolation="bilinear")
 70         plt.axis("off")
 71         self.textBrowser.setHtml('<img src="bilibiili_词云.png">')
 72 
 73     def retranslateUi(self, Form):
 74         _translate = QtCore.QCoreApplication.translate
 75         Form.setWindowTitle(_translate("Form", "Form"))
 76         self.label.setText(_translate("Form",
 77                                       "<html><head/><body><p align=\"center\"><span style=\" font-size:24pt; font-weight:600; color:#00aaff;\">弹幕词云生成</span></p></body></html>"))
 78         self.label_3.setText(_translate("Form", "输入你的视频链接"))
 79         # self.pushButton.setText(_translate("Form", "确认"))
 80         self.label_2.setText(_translate("Form",
 81                                         "<html><head/><body><p><span style=\" font-size:18pt;\">分析得出的词云图如下</span></p></body></html>"))
 82         self.pushButton_2.setText(_translate("Form", "确认"))
 83         self.textBrowser.setHtml(_translate("Form",
 84                                             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
 85                                             "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
 86                                             "p, li { white-space: pre-wrap; }\n"
 87                                             "</style></head><body style=\" font-family:\'SimSun\'; font-size:11.25pt; font-weight:400; font-style:normal;\">\n"
 88                                             "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
 89 
 90 
 91 if __name__ == "__main__":
 92     import sys
 93 
 94     app = QtWidgets.QApplication(sys.argv)
 95     Form = QtWidgets.QWidget()
 96     ui = CiYun()
 97 
 98     ui.setupUi(Form)
 99 
100     Form.show()
101 
102     sys.exit(app.exec_())

danmuxinxi_ui.py 文件

  1 import re
  2 import pandas as pd
  3 import requests
  4 from PyQt5 import QtCore, QtGui, QtWidgets
  5 
  6 
  7 class DanMu(QtWidgets.QMainWindow):
  8     def __init__(self):
  9         super(DanMu, self).__init__()
 10         self.setupUi(self)
 11         self.retranslateUi(self)
 12 
 13     def setupUi(self, Form):
 14         Form.setObjectName("Form")
 15         Form.resize(599, 672)
 16         self.label = QtWidgets.QLabel(Form)
 17         self.label.setGeometry(QtCore.QRect(10, 10, 251, 61))
 18         self.label.setStyleSheet("background-color: rgb(85, 0, 0);")
 19         self.label.setObjectName("label")
 20         self.label_2 = QtWidgets.QLabel(Form)
 21         self.label_2.setGeometry(QtCore.QRect(40, 150, 261, 41))
 22         self.label_2.setStyleSheet("background-color: rgb(142, 229, 255);")
 23         self.label_2.setObjectName("label_2")
 24         self.textBrowser = QtWidgets.QTextBrowser(Form)
 25         self.textBrowser.setGeometry(QtCore.QRect(40, 200, 501, 421))
 26         self.textBrowser.setObjectName("textBrowser")
 27         self.lineEdit = QtWidgets.QLineEdit(Form)
 28         self.lineEdit.setGeometry(QtCore.QRect(40, 110, 401, 31))
 29         self.lineEdit.setObjectName("lineEdit")
 30         self.label_3 = QtWidgets.QLabel(Form)
 31         self.label_3.setGeometry(QtCore.QRect(40, 84, 121, 21))
 32         self.label_3.setObjectName("label_3")
 33         self.pushButton = QtWidgets.QPushButton(Form)
 34         self.pushButton.setGeometry(QtCore.QRect(450, 110, 91, 31))
 35         self.pushButton.setObjectName("pushButton")
 36 
 37         # 设置获取bvurl
 38         # 设置button单击事件 点击后调用get_danmu的方法
 39         self.pushButton.clicked.connect(self.get_danmu)
 40 
 41         self.retranslateUi(Form)
 42         QtCore.QMetaObject.connectSlotsByName(Form)
 43 
 44     def __getXMlUrl(self, reponse_text):
 45         match_rule = r'cid=(.*?)&aid'
 46         oid = re.search(match_rule, reponse_text).group().replace('cid=', '').replace('&aid', '')
 47 
 48         # 通过该 `oid` 参数获得xml的链接
 49         xml_url = 'https://api.bilibili.com/x/v1/dm/list.so?oid=' + oid
 50         return xml_url
 51 
 52     def get_danmu(self):
 53         # 获取弹幕数据
 54         bv_url = self.lineEdit.text()
 55         headers = {
 56             "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"}
 57         resp = requests.get(bv_url, headers=headers)
 58         getXMlUrlFun = self.__getXMlUrl
 59         danmu_url = getXMlUrlFun(resp.text)
 60 
 61         danmu = requests.get(url=danmu_url, headers=headers).content.decode()
 62         # print(danmu)
 63         danmu_list = re.findall('<d p=".*?">(.*?)</d>', danmu)
 64         # print(danmu_list)
 65         # 处理弹幕数据 抓出前十
 66         danmu_var = pd.Series(danmu_list)
 67         # print(danmu_var)
 68         value = danmu_var.value_counts()
 69         # print(value)
 70         # 获取排名前十的弹幕值
 71         danmu_dict = {}
 72         for i in range(10):
 73             danmu_data = value.index[i]
 74             # print(danmu_data)
 75             danmu_num = value[danmu_data]
 76             danmu_dict_ = {danmu_data: danmu_num}
 77             danmu_dict = {**danmu_dict, **danmu_dict_}
 78         print(danmu_dict)
 79 
 80         danmu_text = ""
 81         for nu in range(10):
 82             danmu_ = "弹幕排名第" + str(nu + 1) + ": " + '"' + str(list(danmu_dict)[nu]) + '"' + " " * 10 + "数量为" + str(
 83                 list(danmu_dict.values())[nu]) + "\n"
 84             danmu_text = danmu_text + danmu_
 85         print(danmu_text)
 86         self.textBrowser.setText(danmu_text)
 87 
 88     ############################################################
 89     def retranslateUi(self, Form):
 90         _translate = QtCore.QCoreApplication.translate
 91         Form.setWindowTitle(_translate("Form", "Form"))
 92         self.label.setText(_translate("Form",
 93                                       "<html><head/><body><p align=\"center\"><span style=\" font-size:24pt; font-weight:600; color:#00aaff;\">弹幕信息爬取</span></p></body></html>"))
 94         self.label_2.setText(_translate("Form",
 95                                         "<html><head/><body><p><span style=\" font-size:18pt; font-weight:600;\">排名前十的弹幕是</span></p></body></html>"))
 96         self.label_3.setText(_translate("Form", "输入你的视频链接"))
 97         self.pushButton.setText(_translate("Form", "确认"))
 98 
 99 
100 if __name__ == "__main__":
101     import sys
102 
103     app = QtWidgets.QApplication(sys.argv)
104     Form = QtWidgets.QWidget()
105     ui = DanMu()
106     ui.setupUi(Form)
107 
108     Form.show()
109     sys.exit(app.exec_())

video_fengmian_ui.py文件

 1 import requests
 2 from PyQt5 import QtCore, QtGui, QtWidgets
 3 from lxml import etree
 4 import easygui
 5 
 6 
 7 class FengMian(QtWidgets.QMainWindow):
 8     def __init__(self):
 9         super(FengMian, self).__init__()
10         self.setupUi(self)
11         self.retranslateUi(self)
12 
13     def setupUi(self, Form):
14         Form.setObjectName("Form")
15         Form.resize(599, 672)
16         self.label = QtWidgets.QLabel(Form)
17         self.label.setGeometry(QtCore.QRect(0, 0, 251, 61))
18         self.label.setStyleSheet("background-color: rgb(85, 0, 0);")
19         self.label.setObjectName("label")
20         self.lineEdit = QtWidgets.QLineEdit(Form)
21         self.lineEdit.setGeometry(QtCore.QRect(0, 100, 401, 31))
22         self.lineEdit.setObjectName("lineEdit")
23         self.label_2 = QtWidgets.QLabel(Form)
24         self.label_2.setGeometry(QtCore.QRect(10, 140, 261, 81))
25         self.label_2.setStyleSheet("background-color: rgb(142, 229, 255);")
26         self.label_2.setObjectName("label_2")
27         self.label_3 = QtWidgets.QLabel(Form)
28         self.label_3.setGeometry(QtCore.QRect(0, 74, 121, 21))
29         self.label_3.setObjectName("label_3")
30         self.pushButton_2 = QtWidgets.QPushButton(Form)
31         self.pushButton_2.setGeometry(QtCore.QRect(410, 100, 91, 31))
32         self.pushButton_2.setObjectName("pushButton_2")
33         self.textBrowser = QtWidgets.QTextBrowser(Form)
34         self.textBrowser.setGeometry(QtCore.QRect(10, 240, 561, 381))
35         self.textBrowser.setObjectName("textBrowser")
36 
37         # 设置单击事件
38         self.pushButton_2.clicked.connect(self.get_video_fm)
39 
40         self.retranslateUi(Form)
41         QtCore.QMetaObject.connectSlotsByName(Form)
42 
43     # 自定义函数 供单击事件调用
44     def get_video_fm(self):
45         bv_url = self.lineEdit.text()
46         headers = {
47             "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36"}
48         resp = requests.get(bv_url, headers=headers)
49         html = resp.text
50         # print(html)
51         html_tree = etree.HTML(html)
52         image_url = html_tree.xpath('//meta[@itemprop="thumbnailUrl"]/@content')[0]
53         print(image_url)
54         image_content = requests.get(url=image_url, headers=headers).content
55         with open('video_image.jpg', 'wb') as jpg:
56             jpg.write(image_content)
57         easygui.msgbox("视频封面已经保存到当前目录-- video_image.jpg")
58         self.textBrowser.setHtml('<img src="video_image.jpg" height="379" width="550">')
59 
60     def retranslateUi(self, Form):
61         _translate = QtCore.QCoreApplication.translate
62         Form.setWindowTitle(_translate("Form", "Form"))
63         self.label.setText(_translate("Form",
64                                       "<html><head/><body><p align=\"center\"><span style=\" font-size:24pt; font-weight:600; color:#00aaff;\">视频封面获取</span></p></body></html>"))
65         self.label_2.setText(_translate("Form",
66                                         "<html><head/><body><p><span style=\" font-size:18pt;\">视频的封面图如下</span></p><p><span style=\" font-size:18pt;\">并且已保存到当前目录</span></p></body></html>"))
67         self.label_3.setText(_translate("Form", "输入你的视频链接"))
68         self.pushButton_2.setText(_translate("Form", "确认"))
69         self.textBrowser.setHtml(_translate("Form",
70                                             "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
71                                             "<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
72                                             "p, li { white-space: pre-wrap; }\n"
73                                             "</style></head><body style=\" font-family:\'SimSun\'; font-size:11.25pt; font-weight:400; font-style:normal;\">\n"
74                                             "<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><br /></p></body></html>"))
75 
76 
77 if __name__ == "__main__":
78     import sys
79 
80     app = QtWidgets.QApplication(sys.argv)
81     Form = QtWidgets.QWidget()
82     ui = FengMian()
83 
84     ui.setupUi(Form)
85 
86     Form.show()
87 
88     sys.exit(app.exec_())

video_info_ui.py 文件

  1 import re
  2 import time
  3 from bs4 import BeautifulSoup
  4 import requests
  5 from PyQt5 import QtCore, QtGui, QtWidgets
  6 import openpyxl
  7 import easygui
  8 
  9 
 10 class VideoInfo(QtWidgets.QMainWindow):
 11     def __init__(self):
 12         super(VideoInfo, self).__init__()
 13         self.setupUi(self)
 14         self.retranslateUi(self)
 15 
 16     def setupUi(self, Form):
 17         Form.setObjectName("Form")
 18         Form.resize(599, 672)
 19         self.label_2 = QtWidgets.QLabel(Form)
 20         self.label_2.setGeometry(QtCore.QRect(40, 180, 281, 91))
 21         self.label_2.setStyleSheet("background-color: rgb(142, 229, 255);")
 22         self.label_2.setObjectName("label_2")
 23         self.label_3 = QtWidgets.QLabel(Form)
 24         self.label_3.setGeometry(QtCore.QRect(40, 114, 171, 21))
 25         self.label_3.setObjectName("label_3")
 26         self.pushButton_2 = QtWidgets.QPushButton(Form)
 27         self.pushButton_2.setGeometry(QtCore.QRect(450, 140, 91, 31))
 28         self.pushButton_2.setObjectName("pushButton_2")
 29         self.label = QtWidgets.QLabel(Form)
 30         self.label.setGeometry(QtCore.QRect(40, 40, 501, 61))
 31         self.label.setStyleSheet("background-color: rgb(85, 0, 0);")
 32         self.label.setObjectName("label")
 33         self.lineEdit = QtWidgets.QLineEdit(Form)
 34         self.lineEdit.setGeometry(QtCore.QRect(40, 140, 401, 31))
 35         self.lineEdit.setObjectName("lineEdit")
 36 
 37         # 设置单击事件 调用get_video_info方法
 38         self.pushButton_2.clicked.connect(self.get_video_info)
 39 
 40         self.retranslateUi(Form)
 41         QtCore.QMetaObject.connectSlotsByName(Form)
 42 
 43     def get_video_info(self):
 44         key_word = self.lineEdit.text()
 45         wb = openpyxl.Workbook()
 46         ws = wb.active
 47         ws.cell(row=1, column=1).value = '标题'
 48         ws.cell(row=1, column=2).value = '视频链接'
 49         ws.cell(row=1, column=3).value = '时长'
 50         ws.cell(row=1, column=4).value = '视频简介'
 51         ws.cell(row=1, column=5).value = '播放量'
 52         ws.cell(row=1, column=6).value = '弹幕量'
 53         ws.cell(row=1, column=7).value = '发布时间'
 54         ws.cell(row=1, column=8).value = 'up主'
 55 
 56         easygui.msgbox("正在爬取中,请耐心等待 ~~ ")
 57         num = 1
 58         row = 2
 59         text = re.compile(r'\s+')
 60         for x in range(10):
 61             url = "https://search.bilibili.com/all?keyword=" + str(key_word) + "&page=" + str(num)
 62             headers = {
 63                 "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36"}
 64             response = requests.get(url=url)
 65             html = response.text
 66             # print(html)
 67             soup = BeautifulSoup(html, "lxml")
 68             # print(soup)
 69             video_list = soup.select('.video-item')
 70             # print(video_list)
 71             for i in video_list:
 72                 soup2 = BeautifulSoup(str(i), "lxml")
 73                 title1 = soup2.select('.img-anchor')[0]["title"]
 74                 title = re.sub(text, '', title1)
 75                 # print(title)
 76                 ws.cell(row=row, column=1).value = title
 77 
 78                 video_href1 = "https:" + soup2.select('.img-anchor')[0]["href"]
 79                 video_href = re.sub(text, '', video_href1)
 80                 # print(video_href)
 81                 ws.cell(row=row, column=2).value = video_href
 82 
 83                 duration1 = soup2.select('.so-imgTag_rb')[0].get_text()
 84                 duration = re.sub(text, '', duration1)
 85                 # print(duration)
 86                 ws.cell(row=row, column=3).value = duration
 87 
 88                 description1 = soup2.select('.des')[0].get_text()
 89                 description = re.sub(text, '', description1)
 90                 # print(description)
 91                 ws.cell(row=row, column=4).value = description
 92 
 93                 watch_number1 = soup2.select('span[title="观看"]')[0].get_text()
 94                 watch_number = re.sub(text, '', watch_number1)
 95                 # print(watch_number)
 96                 ws.cell(row=row, column=5).value = watch_number
 97 
 98                 danmu1 = soup2.select('span[title="弹幕"]')[0].get_text()
 99                 danmu = re.sub(text, '', danmu1)
100                 # print(danmu)
101                 ws.cell(row=row, column=6).value = danmu
102 
103                 date1 = soup2.select('span[title="上传时间"]')[0].get_text()
104                 date = re.sub(text, '', date1)
105                 # print(date)
106                 ws.cell(row=row, column=7).value = date
107 
108                 up1 = soup2.select('span[title="up主"]')[0].get_text()
109                 up = re.sub(text, '', up1)
110                 # print(up)
111                 ws.cell(row=row, column=8).value = up
112                 wb.save('bilibili.xlsx')
113                 row += 1
114             num += 1
115             time.sleep(5)
116         # print("end")
117         # print(num)
118         easygui.msgbox("抓取成功! bilibili.xlsx文件已保存在当前目录下")
119 
120     ###########
121     def retranslateUi(self, Form):
122         _translate = QtCore.QCoreApplication.translate
123         Form.setWindowTitle(_translate("Form", "Form"))
124         self.label_2.setText(_translate("Form",
125                                         "<html><head/><body><p><span style=\" font-size:18pt;\">通过指定搜索关键词分析</span></p><p><span style=\" font-size:18pt;\">得到数据并存储在表格中</span></p></body></html>"))
126         self.label_3.setText(_translate("Form", "输入你的视频搜索关键字"))
127         self.pushButton_2.setText(_translate("Form", "确认"))
128         self.label.setText(_translate("Form",
129                                       "<html><head/><body><p align=\"center\"><span style=\" font-size:24pt; font-weight:600; color:#00aaff;\">视频信息搜索工具</span></p></body></html>"))
130 
131 
132 if __name__ == "__main__":
133     import sys
134 
135     app = QtWidgets.QApplication(sys.argv)
136     Form = QtWidgets.QWidget()
137     ui = VideoInfo()
138 
139     ui.setupUi(Form)
140 
141     Form.show()
142 
143     sys.exit(app.exec_())

代码很烂,可能没有参考价值,个人备份使用

 

posted @ 2021-12-21 18:19  Taylor十万伏特  阅读(134)  评论(0编辑  收藏  举报