• 【爬虫系列】用Pyqt5写一个爬虫小助手


    成品

    在这里插入图片描述

    简介

    PyQt5是Digia的一套Qt5应用框架与python的结合,同时支持2.x和3.x。Qt库由Riverbank Computing开发,是最强大的GUI库之一

    Pyqt5安装

    pip install pyqt5
    
    • 1

    Designer安装

    作用:将ui文件转换为py文件

    pip install pyqt5-tools
    
    • 1

    文件转换

    C:\Users\Administrator\AppData\Local\Programs\Python\Python37\Lib\site-packages\qt5_applications\Qt\bin
    
    • 1

    可以通过上面目录找下designer.exe
    在这里插入图片描述
    在这里插入图片描述
    打开designer.exe,写一个简单的界面,然后转换为py文件

    转换命令

    pyuic5 -o py文件 Ui文件
    
    • 1

    实现

    Ui设计

    from PyQt5 import QtCore, QtGui, QtWidgets
    # 导入样式
    from QSSwhite import QSS
    
    class Ui_MainWindow(QtWidgets.QMainWindow):
        def __init__(self):
            super().__init__()
            self.setupUi(self)
    
        def setupUi(self, MainWindow):
            MainWindow.setObjectName("MainWindow")
            MainWindow.resize(640, 540)
            MainWindow.setWindowIcon(QtGui.QIcon("jd.png"))
            MainWindow.setMaximumSize(QtCore.QSize(640, 540))
            self.centralwidget = QtWidgets.QWidget(MainWindow)
            self.centralwidget.setObjectName("centralwidget")
            self.tabWidget = QtWidgets.QTabWidget(self.centralwidget)
            self.tabWidget.setGeometry(QtCore.QRect(20, 4, 604, 473))
            self.tabWidget.setObjectName("tabWidget")
            self.tab_1 = QtWidgets.QWidget()
            self.tab_1.setObjectName("tab_1")
            self.groupBox_11 = QtWidgets.QGroupBox(self.tab_1)
            self.groupBox_11.setGeometry(QtCore.QRect(30, 10, 535, 151))
            self.groupBox_11.setObjectName("groupBox_11")
            self.label_11 = QtWidgets.QLabel(self.groupBox_11)
            self.label_11.setGeometry(QtCore.QRect(50, 18, 461, 123))
            self.label_11.setObjectName("label_11")
            self.groupBox_12 = QtWidgets.QGroupBox(self.tab_1)
            self.groupBox_12.setGeometry(QtCore.QRect(30, 180, 535, 251))
            self.groupBox_12.setObjectName("groupBox_12")
            self.label_4 = QtWidgets.QLabel(self.groupBox_12)
            self.label_4.setGeometry(QtCore.QRect(40, 30, 60, 24))
            self.label_4.setObjectName("label_4")
            self.lineEdit_1 = QtWidgets.QLineEdit(self.groupBox_12)
            self.lineEdit_1.setGeometry(QtCore.QRect(102, 30, 156, 24))
            self.lineEdit_1.setObjectName("lineEdit_1")
            self.radioButton_1 = QtWidgets.QRadioButton(self.groupBox_12)
            self.radioButton_1.setGeometry(QtCore.QRect(270, 26, 110, 32))
            icon = QtGui.QIcon()
            self.radioButton_1.setIcon(icon)
            self.radioButton_1.setIconSize(QtCore.QSize(30, 30))
            self.radioButton_1.setObjectName("radioButton_1")
            self.pushButton_1 = QtWidgets.QPushButton(self.groupBox_12)
            self.pushButton_1.setGeometry(QtCore.QRect(350, 27, 80, 30))
            icon2 = QtGui.QIcon()
            icon2.addPixmap(QtGui.QPixmap("1494068712.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.pushButton_1.setIcon(icon2)
            self.pushButton_1.setIconSize(QtCore.QSize(20, 20))
            self.pushButton_1.setObjectName("pushButton_1")
            self.label_5 = QtWidgets.QLabel(self.groupBox_12)
            self.label_5.setGeometry(QtCore.QRect(40, 72, 71, 24))
            self.label_5.setObjectName("label_5")
            self.progressBar_1 = QtWidgets.QProgressBar(self.groupBox_12)
            self.progressBar_1.setGeometry(QtCore.QRect(105, 75, 326, 20))
            self.progressBar_1.setProperty("value", 0)
            self.progressBar_1.setObjectName("progressBar_1")
            self.textEdit_1 = QtWidgets.QTextEdit(self.groupBox_12)
            self.textEdit_1.setGeometry(QtCore.QRect(40, 120, 455, 121))
            self.textEdit_1.setObjectName("textEdit_1")
            self.tabWidget.addTab(self.tab_1, "")
            self.tab_2 = QtWidgets.QWidget()
            self.tab_2.setObjectName("tab_2")
            self.groupBox_21 = QtWidgets.QGroupBox(self.tab_2)
            self.groupBox_21.setGeometry(QtCore.QRect(30, 180, 535, 251))
            self.groupBox_21.setObjectName("groupBox_21")
            self.lineEdit_2 = QtWidgets.QLineEdit(self.groupBox_21)
            self.lineEdit_2.setGeometry(QtCore.QRect(102, 30, 156, 24))
            self.lineEdit_2.setObjectName("lineEdit_2")
            self.label_6 = QtWidgets.QLabel(self.groupBox_21)
            self.label_6.setGeometry(QtCore.QRect(40, 30, 60, 24))
            self.label_6.setObjectName("label_6")
            self.label_7 = QtWidgets.QLabel(self.groupBox_21)
            self.label_7.setGeometry(QtCore.QRect(160, 72, 71, 24))
            self.label_7.setObjectName("label_7")
            self.pushButton_2 = QtWidgets.QPushButton(self.groupBox_21)
            self.pushButton_2.setGeometry(QtCore.QRect(40, 70, 91, 30))
            self.pushButton_2.setIcon(icon2)
            self.pushButton_2.setIconSize(QtCore.QSize(20, 20))
            self.pushButton_2.setObjectName("pushButton_2")
            self.progressBar_2 = QtWidgets.QProgressBar(self.groupBox_21)
            self.progressBar_2.setGeometry(QtCore.QRect(240, 75, 250, 20))
            self.progressBar_2.setProperty("value", 0)
            self.progressBar_2.setObjectName("progressBar_2")
            self.textEdit_2 = QtWidgets.QTextEdit(self.groupBox_21)
            self.textEdit_2.setGeometry(QtCore.QRect(40, 120, 455, 121))
            self.textEdit_2.setObjectName("textEdit_2")
            self.groupBox_22 = QtWidgets.QGroupBox(self.tab_2)
            self.groupBox_22.setGeometry(QtCore.QRect(30, 10, 535, 151))
            self.groupBox_22.setObjectName("groupBox_22")
            self.label_2 = QtWidgets.QLabel(self.groupBox_22)
            self.label_2.setGeometry(QtCore.QRect(50, 18, 461, 123))
            self.label_2.setObjectName("label_2")
            self.tabWidget.addTab(self.tab_2, "")
            self.tab_3 = QtWidgets.QWidget()
            self.tab_3.setObjectName("tab_3")
            self.groupBox_31 = QtWidgets.QGroupBox(self.tab_3)
            self.groupBox_31.setGeometry(QtCore.QRect(30, 180, 535, 251))
            self.groupBox_31.setObjectName("groupBox_31")
            self.progressBar_3 = QtWidgets.QProgressBar(self.groupBox_31)
            self.progressBar_3.setGeometry(QtCore.QRect(240, 75, 250, 20))
            self.progressBar_3.setProperty("value", 0)
            self.progressBar_3.setObjectName("progressBar_3")
            self.pushButton_3 = QtWidgets.QPushButton(self.groupBox_31)
            self.pushButton_3.setGeometry(QtCore.QRect(40, 70, 91, 30))
            self.pushButton_3.setIcon(icon2)
            self.pushButton_3.setIconSize(QtCore.QSize(20, 20))
            self.pushButton_3.setObjectName("pushButton_3")
            self.label_8 = QtWidgets.QLabel(self.groupBox_31)
            self.label_8.setGeometry(QtCore.QRect(40, 30, 60, 24))
            self.label_8.setObjectName("label_8")
            self.lineEdit_31 = QtWidgets.QLineEdit(self.groupBox_31)
            self.lineEdit_31.setGeometry(QtCore.QRect(102, 30, 156, 24))
            self.lineEdit_31.setObjectName("lineEdit_31")
            self.textEdit_3 = QtWidgets.QTextEdit(self.groupBox_31)
            self.textEdit_3.setGeometry(QtCore.QRect(40, 120, 455, 121))
            self.textEdit_3.setObjectName("textEdit_3")
            self.label_9 = QtWidgets.QLabel(self.groupBox_31)
            self.label_9.setGeometry(QtCore.QRect(160, 72, 71, 24))
            self.label_9.setObjectName("label_9")
            self.label_10 = QtWidgets.QLabel(self.groupBox_31)
            self.label_10.setGeometry(QtCore.QRect(272, 30, 72, 24))
            self.label_10.setObjectName("label_10")
            self.lineEdit_32 = QtWidgets.QLineEdit(self.groupBox_31)
            self.lineEdit_32.setGeometry(QtCore.QRect(348, 30, 146, 24))
            self.lineEdit_32.setObjectName("lineEdit_32")
            self.groupBox_32 = QtWidgets.QGroupBox(self.tab_3)
            self.groupBox_32.setGeometry(QtCore.QRect(30, 10, 535, 151))
            self.groupBox_32.setObjectName("groupBox_32")
            self.label_3 = QtWidgets.QLabel(self.groupBox_32)
            self.label_3.setGeometry(QtCore.QRect(50, 18, 461, 123))
            self.label_3.setObjectName("label_3")
            self.tabWidget.addTab(self.tab_3, "")
            MainWindow.setCentralWidget(self.centralwidget)
            self.menubar = QtWidgets.QMenuBar(MainWindow)
            self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 23))
            self.menubar.setObjectName("menubar")
            self.menu_F = QtWidgets.QMenu(self.menubar)
            self.menu_F.setObjectName("menu_F")
            self.menu_H = QtWidgets.QMenu(self.menubar)
            self.menu_H.setObjectName("menu_H")
            MainWindow.setMenuBar(self.menubar)
            self.statusbar = QtWidgets.QStatusBar(MainWindow)
            self.statusbar.setObjectName("statusbar")
            MainWindow.setStatusBar(self.statusbar)
            self.actionOpenfile = QtWidgets.QAction(MainWindow)
            icon3 = QtGui.QIcon()
            self.actionOpenfile.setIcon(icon3)
            self.actionOpenfile.setObjectName("actionOpenfile")
            self.actionQiut = QtWidgets.QAction(MainWindow)
            icon4 = QtGui.QIcon()
            self.actionQiut.setIcon(icon4)
            self.actionQiut.setObjectName("actionQiut")
            self.actionAbout = QtWidgets.QAction(MainWindow)
            icon5 = QtGui.QIcon()
            icon5.addPixmap(QtGui.QPixmap("201752212221.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.actionAbout.setIcon(icon5)
            self.actionAbout.setObjectName("actionAbout")
            self.actionAuthor = QtWidgets.QAction(MainWindow)
            icon6 = QtGui.QIcon()
            icon6.addPixmap(QtGui.QPixmap("2017522122216.png"), QtGui.QIcon.Normal, QtGui.QIcon.Off)
            self.actionAuthor.setIcon(icon6)
            self.actionAuthor.setObjectName("actionAuthor")
            self.menu_F.addAction(self.actionOpenfile)
            self.menu_F.addSeparator()
            self.menu_F.addAction(self.actionQiut)
            self.menu_H.addAction(self.actionAbout)
            self.menu_H.addSeparator()
            self.menu_H.addAction(self.actionAuthor)
            self.menubar.addAction(self.menu_F.menuAction())
            self.menubar.addAction(self.menu_H.menuAction())
    
            self.retranslateUi(MainWindow)
            self.tabWidget.setCurrentIndex(0)
            QtCore.QMetaObject.connectSlotsByName(MainWindow)
    
        def retranslateUi(self, MainWindow):
            _translate = QtCore.QCoreApplication.translate
            MainWindow.setWindowTitle(_translate("MainWindow", "京东助手"))
            self.groupBox_11.setTitle(_translate("MainWindow", "爬取说明"))
            self.label_11.setText(_translate("MainWindow", "

    ⑴、爬取京东商品评分数据。

    ⑵、同目录下创建一个txt,并添加商品ID。

    ⑶、在文件输入框中输入放有商品ID的文件名称。

    "
    )) self.groupBox_12.setTitle(_translate("MainWindow", "爬取过程")) self.label_4.setText(_translate("MainWindow", "文件名称:")) self.radioButton_1.setText(_translate("MainWindow", "JD")) self.radioButton_1.setChecked(True) #设置默认选中 self.pushButton_1.setText(_translate("MainWindow", "开始")) self.label_5.setText(_translate("MainWindow", "爬取进度:")) self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_1), _translate("MainWindow", "获取评分")) self.groupBox_21.setTitle(_translate("MainWindow", "爬取过程")) # 添加默认值 self.lineEdit_1.setText("ids.txt") self.lineEdit_32.setText("images") #设置鼠标样式和提示语 for each in [self.pushButton_1,self.pushButton_2,self.pushButton_3]: each.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor)) # 添加样式风格 self.mystyle() def mystyle(self): qss = QSS() self.setStyleSheet(qss.White)
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204

    Ui样式

    # -*- coding: utf-8 -*-
    
    class QSS():
        def __init__(self):
            self.White = '''/**********主界面样式**********/
    QWidget#MainWindow {
            font-family:Microsoft YaHei;
            font-size:13px;
            border: 0px solid rgb(111, 156, 207);
            background: rgb(130, 241, 252);
    }
    QWidget#messageWidget {
            background: rgba(173, 202, 232, 50%);
    }
    /**********菜单栏**********/
    QMenuBar {
            font-size:13px;
            background: rgb(232, 241, 252);
            border: 1px solid rgb(200, 156, 207);
            border-left: none;
            border-right: none;
    }
    QMenuBar::item {
            font-size:13px;
            font-family:Microsoft YaHei;
            border: 0px solid transparent;
            padding: 5px 10px 5px 10px;
            background: transparent;
    }
    QMenuBar::item:enabled {
            font-size:13px;
            color: rgb(2, 65, 132);
    }
    QMenuBar::item:!enabled {
            color: rgb(155, 155, 155);
    }
    QMenuBar::item:enabled:selected {
            border-top-color: rgb(118, 156, 207);
            border-bottom-color: rgb(118, 156, 207);
            background: rgb(118, 156, 207);
    }
    /**********菜单**********/
    QMenu {
            font-size:13px;
            font-family:Microsoft YaHei;
            border: 1px solid rgb(111, 156, 207);
            background: rgb(200, 156, 207);
    }
    QMenu::item {
            font-family:Microsoft YaHei;
            height: 18px;
            padding: 5px 25px 5px 20px;
            padding-left: 30px;
            padding-right: 12px;
    }
    QMenu::item:enabled {
            color: rgb(84, 84, 84);
    }
    QMenu::item:!enabled {
            color: rgb(155, 155, 155);
    }
    QMenu::item:enabled:selected {
            color: rgb(2, 65, 132);
            background: rgba(255, 255, 255, 200);
    }
    QMenu::separator {
            height: 1px;
            background: rgb(111, 156, 207);
    }
    QMenu::indicator {
            width: 13px;
            height: 13px;
    }
    QMenu::icon {
            padding-left: 12px;
            padding-right: 0px;
    }
    
    /**********状态栏**********/
    QStatusBar {
            font-size:13px;
            font-family:Microsoft YaHei;
            background: rgb(187, 212, 238);
            border: 1px solid rgb(111, 156, 207);
            border-left: none;
            border-right: none;
            border-bottom: none;
    }
    QStatusBar::item {
            font-family:Microsoft YaHei;
            border: none;
            border-right: 1px solid rgb(111, 156, 207);
    }
    /**********分组框**********/
    QGroupBox {
            font-size:13px;
            font-family:Microsoft YaHei;
            border: 1px solid rgb(111, 156, 207);
            border-radius: 4px;
            margin-top: 10px;
    }
    QGroupBox::title {
            font-family:Microsoft YaHei;
            color: rgb(56, 99, 154);
            top: -10px;
            left: 10px;
    }
    /**********滚动条-垂直**********/
    QScrollBar:vertical {
            width: 20px;
            background: transparent;
            margin-left: 3px;
            margin-right: 3px;
    }
    QScrollBar::handle:vertical {
            width: 20px;
            min-height: 30px;
            background: rgb(170, 200, 230);
            margin-top: 15px;
            margin-bottom: 15px;
    }
    QScrollBar::handle:vertical:hover {
            background: rgb(165, 195, 225);
    }
    QScrollBar::sub-line:vertical {
            height: 15px;
            background: transparent;
            image: url("White/topArrow.png");
            subcontrol-position: top;
    }
    QScrollBar::add-line:vertical {
            height: 15px;
            background: transparent;
            image: url("White/bottomArrow.png");
            subcontrol-position: bottom;
    }
    QScrollBar::sub-line:vertical:hover {
            background: rgb(170, 200, 230);
    }
    QScrollBar::add-line:vertical:hover {
            background: rgb(170, 200, 230);
    }
    QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {
            background: transparent;
    }
    
    QScrollBar#verticalScrollBar:vertical {
            margin-top: 30px;
    }
    /**********页签项**********/
    QTabWidget::pane {
            font-family:Microsoft YaHei;
            border: none;
            border-top: 3px solid rgb(0, 78, 161);
            background: rgb(187, 212, 238);
    }
    QTabWidget::tab-bar {
            border: none;
    }
    QTabBar::tab {
            font-family:Microsoft YaHei;
            border: none;
            border-top-left-radius: 4px;
            border-top-right-radius: 4px;
            color: white;
            background: rgb(120, 170, 220);
            height: 28px;
            min-width: 85px;
            margin-right: 5px;
            padding-left: 5px;
            padding-right: 5px;
    }
    QTabBar::tab:hover {
            background: rgb(0, 78, 161);
    }
    QTabBar::tab:selected {
            color: white;
            background: rgb(0, 78, 161);
    }
    
    QTabWidget#tabWidget::pane {
            border: 1px solid rgb(111, 156, 207);
            background: rgb(232, 241, 252);
            margin-top: -1px;
    }
    
    QTabBar#tabBar::tab {
            border: 1px solid rgb(111, 156, 207);
            border-bottom: none;
            color: rgb(70, 71, 73);
            background: transparent;
    }
    QTabBar#tabBar::tab:hover {
            color: rgb(2, 65, 132);
    }
    QTabBar#tabBar::tab:selected {
            color: rgb(2, 65, 132);
            background: rgb(232, 241, 252);
    }
    /**********进度条**********/
    QProgressBar {
            border: none;
            text-align: center;
            color: white;
            background-color: transparent;
            background-image: url("White/progressBar.png");
            background-repeat: repeat-x;
    }
    QProgressBar::chunk {
            border: none;
            background-color: transparent;
            background-image: url("White/progressBarChunk.png");
            background-repeat: repeat-x;
    }
    /**********单选框**********/
    QRadioButton{
            font-size:13px;
            spacing: 5px;
    }
    QRadioButton:enabled:checked{
            color: rgb(2, 65, 132);
    }
    QRadioButton:enabled:!checked{
            color: rgb(70, 71, 73);
    }
    QRadioButton:enabled:hover{
            color: rgb(0, 78, 161);
    }
    QRadioButton:!enabled{
            color: rgb(80, 80, 80);
    }
    QRadioButton::indicator {
            width: 20px;
            height: 20px;
    }
    QRadioButton::indicator:unchecked {
            image: url("White/radioButton.png");
    }
    QRadioButton::indicator:unchecked:hover {
            image: url("White/radioButtonHover.png");
    }
    QRadioButton::indicator:unchecked:pressed {
            image: url("White/radioButtonPressed.png");
    }
    QRadioButton::indicator:checked {
            image: url("White/radioButtonChecked.png");
    }
    QRadioButton::indicator:checked:hover {
            image: url("White/radioButtonCheckedHover.png");
    }
    QRadioButton::indicator:checked:pressed {
            image: url("White/radioButtonCheckedPressed.png");
    }
    /**********输入框**********/
    QLineEdit {
            border-radius: 4px;
            height: 25px;
            border: 1px solid rgb(111, 156, 207);
            background: white;
    }
    QLineEdit:enabled {
            color: rgb(84, 84, 84);
    }
    QLineEdit:enabled:hover, QLineEdit:enabled:focus {
            color: rgb(51, 51, 51);
    }
    QLineEdit:!enabled {
            color: rgb(80, 80, 80);
    }
    /**********文本编辑框**********/
    QTextEdit {
            font-family:Microsoft YaHei;
            font:12px;
            border: 1px solid rgb(111, 156, 207);
            color: rgb(70, 71, 73);
            background: rgb(187, 212, 238);
    }
    /**********滚动区域**********/
    QScrollArea {
            border: 1px solid rgb(111, 156, 207);
            background: rgb(187, 212, 238);
    }
    /**********滚动区域**********/
    QWidget#transparentWidget {
            background: transparent;
    }
    /**********标签**********/
    QLabel {
            font-family:Microsoft YaHei;
            font:13px;
            color: rgb(0, 160, 230);
    }
    /**********按钮**********/
    QPushButton{
            border-radius: 4px;
            border: none;
            width: 75px;
            height: 25px;
    }
    QPushButton:enabled {
            background: rgb(120, 170, 220);
            color: white;
    }
    QPushButton:!enabled {
            background: rgb(180, 180, 180);
            color: white;
    }
    QPushButton:enabled:hover{
            background: rgb(100, 140, 230);
    }
    QPushButton:enabled:pressed{
            background: rgb(0, 78, 161);
    }'''
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
    • 310
    • 311
    • 312
    • 313

    Spider

    import requests
    import json
    import re
    from lxml import etree
    
    class Spiders(object):
        def __init__(self):
            self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36'}
    
        # 形成富文本,参数是文本内容和字体颜色
        def getmsg(self,themsg,thecolor):
            msg = '

    {}

    '
    .format(thecolor,themsg) return msg # 读取数据 def get_Infos(self, filename): Infos = [] with open(filename, 'r') as f: data = f.readlines() for each in data: if len(each.strip()) > 0: Infos.append(each.strip()) return Infos # 获取京东评分 def get_JD(self, id, outfile): url = 'https://club.jd.com/comment/productCommentSummaries.action?referenceIds=%s' % id # DSR_URL res = requests.get(url, headers=self.headers) dic = json.loads(res.text)['CommentsCount'][0] SkuId = str(dic['SkuId']).strip() # 商品ID CommentCountStr = str(dic['CommentCountStr']) # 累计评价 GoodRate = str(dic['GoodRate']).strip() # 好评率 GoodCount = str(dic['GoodCountStr']).strip() # 好评数 GeneralCount = str(dic['GeneralCountStr']).strip() # 中评数 PoorCount = str(dic['PoorCountStr']).strip() # 差评数 PoorRate = str(dic['PoorRate']).strip() # 差评率 AfterCountStr = str(dic['AfterCountStr']).strip() # 追评 DSR = SkuId + ','+CommentCountStr + ',' + GoodRate + ',' + GoodCount + ',' + GeneralCount + ',' + PoorCount + ',' + PoorRate + ',' + AfterCountStr with open(outfile, 'a',encoding='utf8') as f: f.write(DSR + '\n') return
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45

    主程序

    # -*- coding: utf-8 -*-
    
    from PyQt5 import QtCore, QtWidgets
    from ToolUI import Ui_MainWindow
    import sys
    from APIs import Spiders
    import datetime
    import time
    import os.path
    
    
    class MainUI(Ui_MainWindow):
        def __init__(self):
            super().__init__()
            
            self.pushButton_1.clicked.connect(self.startdsr)  # DSR开始按钮
    
        # 重写关闭函数
        def closeEvent(self, event):
            reply = QtWidgets.QMessageBox.question(self, '关闭程序',
                                                   "关闭程序可能导致正在进行的操作终止,请确认\n是否退出并关闭程序?",
                                                   QtWidgets.QMessageBox.Yes, QtWidgets.QMessageBox.No)
            if reply == QtWidgets.QMessageBox.Yes:
                event.accept()
            else:
                event.ignore()
    
        # 消息框函数,传入2个参数,第1个是标题,第2个是显示内容
        def selectInfo(self, thetitle, megs):
            QtWidgets.QMessageBox.about(self, thetitle, megs)
    
        # 商品类型
        def changePD(self):
            if self.radioButton_1.isChecked():
                product = "jingdong"
            return product
    
        # 更新状态栏
        def statusshow(self, astr):
            self.statusbar.showMessage(astr)
    
        # 启动线程的槽函数
        def startdsr(self):
            self.statusbar.setStyleSheet("color:red")
            self.pushButton_1.setDisabled(True)  # 线程启动锁定按钮
            self.textEdit_1.setText("")  # 插入一个空白,每次启动线程都可以清屏
            txtname = self.lineEdit_1.text()
            product = self.changePD()
            self.dsrthread = dsrThread(txtname, product)
            self.dsrthread.status_signal.connect(self.statusshow)
            self.dsrthread.dsrtext_signal.connect(self.dsrtextshow)
            self.dsrthread.dsrprogmax_signal.connect(self.dsrprog_max)
            self.dsrthread.dsrprog_signal.connect(self.dsrprog_value)
            self.dsrthread.finished.connect(self.dsrpushon)  # 线程结束执行函数
            self.dsrthread.start()
    
        # 线程结束后开启按钮
        def dsrpushon(self):
            self.pushButton_1.setDisabled(False)
    
        # 更新输出文本
        def dsrtextshow(self, astr):
            self.textEdit_1.append(astr)
    
        # 获取进度条最大值
        def dsrprog_max(self, n):
            self.progressBar_1.setMinimum(0)
            self.progressBar_1.setMaximum(n)
    
        # 更新进度条
        def dsrprog_value(self, i):
            self.progressBar_1.setValue(i)
    
    # 线程
    class dsrThread(QtCore.QThread):
        status_signal = QtCore.pyqtSignal(str)  # 发送给状态栏的信号
        dsrtext_signal = QtCore.pyqtSignal(str)  # 发送给输出框的信号
        dsrprogmax_signal = QtCore.pyqtSignal(int)  # 发送给进度条的信号,给出最大值
        dsrprog_signal = QtCore.pyqtSignal(int)  # 发送给进度条的信号,给出每次刷新的进度
    
        def __init__(self, txtname, product):  # 参数:读取的文件名,商品类型
            super().__init__()
            self.txtname = txtname
            self.product = product
            self.api = Spiders()
    
        def run(self):
            start = time.time()
            T = datetime.datetime.now()
            self.status_signal.emit("当前状态:正在进行评分提取操作...")
            try:
                IDs = self.api.get_Infos(self.txtname)
            except:
                self.dsrtext_signal.emit(self.api.getmsg("读取文件失败,请检查文件名称是否有误!", "red"))
            else:
                nums = len(IDs)
                self.dsrprogmax_signal.emit(nums)
                i = 1
                if self.product == "jingdong":
                    outfile = "JD_" + T.strftime("%Y%m%d%H%M") + "_" + str(nums) + ".csv"
                    self.dsrtext_signal.emit(self.api.getmsg("商品类型为【京东商品】,有效ID总计{}个,开始提取评分".format(nums), "#464749"))
                    with open(outfile, 'w',encoding='utf8') as f:
                        f.write('SKU_id,评论数,好评率,好评数,中评数,差评数,差评率,追评\n')
                    for each in IDs:
                        try:
                            self.api.get_JD(each, outfile)
                        except:
                            msg_b = "总计{}个商品ID,第{}个:{}写入信息失败!".format(nums, i, each)
                            self.dsrtext_signal.emit(self.api.getmsg(msg_b, "red"))
                        else:
                            msg_c = "总计{}个商品ID,成功写入第{}个:{}".format(nums, i, each)
                            self.dsrtext_signal.emit(self.api.getmsg(msg_c, "#464749"))
                            self.dsrprog_signal.emit(i)
                        i += 1
                end = time.time()
                msg_d = "评分爬取完成,耗时:%0.2f秒!\n数据保存在当前目录下表格  %s  中" % (float(end - start), outfile)
                self.dsrtext_signal.emit(self.api.getmsg(msg_d, "green"))
            self.status_signal.emit("当前状态:评分信息提取操作完毕!")
    
    if __name__ == '__main__':
        app = QtWidgets.QApplication(sys.argv)
        myui = MainUI()
        myui.show()
        sys.exit(app.exec_())
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125

    展示

    在这里插入图片描述

    爬取结果:

    在这里插入图片描述

    开发

    • 主图爬取

    • 图片下载

    在这里插入图片描述

    点关注不迷路,本文若对你有帮助,烦请三连支持一下 ❤️❤️❤️
    各位的支持和认可就是我最大的动力❤️❤️❤️

  • 相关阅读:
    从零开始学习CANoe 系列文章目录汇总
    Spring容器获取Bean的9种方式
    【LeetCode】268. 丢失的数字
    计算机的种类
    你听说过什么是代码本吗? (幽兰代码本初体验)
    Spring是如何管理事务的?@Transactional注解详解
    javase之序列化与反序列化
    算法通关村-----寻找祖先问题
    【C++修炼之路】8. string类详解
    基于类似Kiva的移动机器人的路径规划(Matlab代码实现)
  • 原文地址:https://blog.csdn.net/fun_sn/article/details/126251038