Python分析bilibili視訊彈幕並獲取發表最多的前十條彈幕-改進版本
阿新 • • 發佈:2021-12-21
比上一版本增加了一點點功能、返回功能太麻煩了沒做
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_())
程式碼很爛,可能沒有參考價值,個人備份使用