爬取热榜数据,通过Qt界面显示,代码可直接运行(python3经典编程案例)

Posted cui_yonghua

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取热榜数据,通过Qt界面显示,代码可直接运行(python3经典编程案例)相关的知识,希望对你有一定的参考价值。

代码如下:

# -*- encoding: utf-8 -*-
import sys
from lxml import etree
from requests import get
from PyQt5 import QtCore, QtGui, QtWidgets


HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/75.0.3770.100 Safari/537.36'
}


class Ui_WinMain(object):
    def __init__(self):
        self.all_url = {
            '微博热搜...': 'https://tophub.today/n/KqndgxeLl9',
            '抖音热搜...': 'https://tophub.today/n/K7GdaMgdQy',
            '百度实时热点...': 'https://tophub.today/n/Jb0vmloB1G',
            '知乎热榜...': 'https://tophub.today/n/mproPpoq6O',
            '虎嗅热文...': 'https://tophub.today/n/5VaobgvAj1',
            '哔哩哔哩全站排行...': 'https://tophub.today/n/74KvxwokxM',
            '豆瓣新片...': 'https://tophub.today/n/mDOvnyBoEB'
        }

    def spider(self, hot_url):
        self.textBrowser.clear()
        response = get(url=hot_url, headers=HEADERS)
        html = etree.HTML(response.content.decode('utf-8'))

        for oes_title in range(1, 11):
            try:
                title = html.xpath(f'//*[@id="page"]/div[2]/div[2]/div[1]/div[2]/div/div[1]/table/tbody/tr[{oes_title}]/td[2]/a//text()')[0]
                hot_index = html.xpath(f'//*[@id="page"]/div[2]/div[2]/div[1]/div[2]/div/div[1]/table/tbody/tr[{oes_title}]/td[3]//text()')[0]
                short_title_url = html.xpath(f'//*[@id="page"]/div[2]/div[2]/div[1]/div[2]/div/div[1]/table/tbody/tr[{oes_title}]/td[2]/a//@href')[0]
                title_url = 'https://tophub.today' + short_title_url
                print(f' {hot_index} | {title} {title_url}')
                mt_txt = f' {hot_index} | {title} \\n'
                self.textBrowser.append(mt_txt)

            except Exception as e:
                print(f'出现异常:{e}')

    def setupUi(self, WinMain):
        WinMain.setObjectName("WinMain")
        WinMain.setWindowModality(QtCore.Qt.WindowModal)
        WinMain.setEnabled(True)
        WinMain.resize(660, 700)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(WinMain.sizePolicy().hasHeightForWidth())
        WinMain.setSizePolicy(sizePolicy)
        WinMain.setMaximumSize(QtCore.QSize(660, 700))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 255, 255))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Window, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Base, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 127))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Window, brush)
        WinMain.setPalette(palette)
        font = QtGui.QFont()
        font.setPointSize(16)
        WinMain.setFont(font)
        WinMain.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.textBrowser = QtWidgets.QTextBrowser(WinMain)
        self.textBrowser.setEnabled(True)
        self.textBrowser.setGeometry(QtCore.QRect(10, 60, 641, 631))
        self.textBrowser.setMaximumSize(QtCore.QSize(660, 666))
        font = QtGui.QFont()
        font.setFamily("华文中宋")
        font.setPointSize(18)
        font.setBold(False)
        font.setWeight(50)
        self.textBrowser.setFont(font)
        self.textBrowser.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.textBrowser.setAutoFillBackground(True)
        self.textBrowser.setFrameShape(QtWidgets.QFrame.NoFrame)
        self.textBrowser.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.textBrowser.setTabStopWidth(200)
        self.textBrowser.setObjectName("textBrowser")
        self.comboBox = QtWidgets.QComboBox(WinMain)
        self.comboBox.setGeometry(QtCore.QRect(120, 10, 240, 40))
        self.comboBox.setMaximumSize(QtCore.QSize(320, 40))
        font = QtGui.QFont()
        font.setFamily("华文中宋")
        font.setPointSize(18)
        self.comboBox.setFont(font)
        self.comboBox.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
        self.comboBox.setMouseTracking(False)
        self.comboBox.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.comboBox.setContextMenuPolicy(QtCore.Qt.ActionsContextMenu)
        self.comboBox.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.comboBox.setAutoFillBackground(False)
        self.comboBox.setEditable(False)
        self.comboBox.setMaxCount(20)
        self.comboBox.setInsertPolicy(QtWidgets.QComboBox.InsertAlphabetically)
        self.comboBox.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContentsOnFirstShow)
        self.comboBox.setDuplicatesEnabled(False)
        self.comboBox.setFrame(False)
        self.comboBox.setModelColumn(0)
        self.comboBox.setObjectName("comboBox")
        self.label = QtWidgets.QLabel(WinMain)
        self.label.setGeometry(QtCore.QRect(10, 10, 101, 41))
        font = QtGui.QFont()
        font.setFamily("楷体")
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        font.setStyleStrategy(QtGui.QFont.PreferAntialias)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(WinMain)
        self.pushButton.setGeometry(QtCore.QRect(490, 10, 110, 40))
        self.pushButton.setMinimumSize(QtCore.QSize(110, 0))
        self.pushButton.setMaximumSize(QtCore.QSize(110, 40))
        palette = QtGui.QPalette()
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(85, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Active, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(85, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Inactive, QtGui.QPalette.ButtonText, brush)
        brush = QtGui.QBrush(QtGui.QColor(255, 170, 0))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.Button, brush)
        brush = QtGui.QBrush(QtGui.QColor(120, 120, 120))
        brush.setStyle(QtCore.Qt.SolidPattern)
        palette.setBrush(QtGui.QPalette.Disabled, QtGui.QPalette.ButtonText, brush)
        self.pushButton.setPalette(palette)
        font = QtGui.QFont()
        font.setFamily("Segoe Script")
        font.setPointSize(24)
        font.setItalic(False)
        font.setUnderline(False)
        self.pushButton.setFont(font)
        self.pushButton.setCursor(QtGui.QCursor(QtCore.Qt.OpenHandCursor))
        self.pushButton.setFocusPolicy(QtCore.Qt.WheelFocus)
        self.pushButton.setAutoRepeat(False)
        self.pushButton.setAutoExclusive(False)
        self.pushButton.setAutoDefault(False)
        self.pushButton.setFlat(True)
        self.pushButton.setObjectName("pushButton")

        self.retranslateUi(WinMain)
        self.comboBox.setCurrentIndex(-1)
        QtCore.QMetaObject.connectSlotsByName(WinMain)

    def retranslateUi(self, WinMain):
        _translate = QtCore.QCoreApplication.translate
        WinMain.setWindowTitle(_translate("WinMain", "Today Hot"))
        self.textBrowser.setHtml(_translate("WinMain",
                                            "<!DOCTYPE HTML PUBLIC \\"-//W3C//DTD HTML 4.0//EN\\" \\"http://www.w3.org/TR/REC-html40/strict.dtd\\">\\n"
                                            "<html><head><meta name=\\"qrichtext\\" content=\\"1\\" /><style type=\\"text/css\\">\\n"
                                            "p, li { white-space: pre-wrap; }\\n"
                                            "</style></head><body style=\\" font-family:\\'华文新魏\\'; font-size:16pt; font-weight:400; font-style:normal;\\">\\n"
                                            "<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; font-family:\\'SimSun\\';\\"><br /></p></body></html>"))
        self.label.setText(_translate("WinMain", "获取源:"))
        self.pushButton.setText(_translate("WinMain", "→ 进入"))

    def main_posh(self):
        self.pushButton.clicked.connect(self.post_url)

    def add_url_box(self):
        for hot_url_ in self.all_url:
            self.comboBox.addItem(hot_url_)

    def post_url(self):
        hot_title = self.comboBox.currentText()
        title_url = self.all_url[hot_title]

        self.spider(hot_url=title_url)


def open_html():
    app = QtWidgets.QApplication(sys.argv)
    WinMain = QtWidgets.QWidget()
    ui = Ui_WinMain()
    ui.setupUi(WinMain)

    ui.add_url_box()
    ui.main_posh()
    ui.post_url()

    WinMain.show()
    sys.exit(app.exec_())


if __name__ == "__main__":
    open_html()

以上是关于爬取热榜数据,通过Qt界面显示,代码可直接运行(python3经典编程案例)的主要内容,如果未能解决你的问题,请参考以下文章

11 信息化领域热词分类分析及解释 第五步按目录爬取热词

Python爬虫实战:定时爬取微博热榜信息并存入SqlServer,不再错过每条热搜

qt中可运行,但打包后的exe不能直接运行

预定热榜!!!用python分析taptap游戏评价,生成词云,源码赠送,评论送书包邮

预定热榜!!!用python分析taptap游戏评价,生成词云,源码赠送,评论送书包邮

爬取知乎热搜