主頁 > 後端開發 > python3GUI--微博圖片爬取工具V1.5 By:PyQt5(附原始碼)

python3GUI--微博圖片爬取工具V1.5 By:PyQt5(附原始碼)

2021-09-25 17:14:06 後端開發

文章目錄

  • 一.準備作業
  • 二.預覽
    • 1.啟動
    • 2.搜索
    • 3.開始爬取
    • 4.結果
  • 三.設計流程
    • 1.整體流程
    • 2.UI設計(草圖)
    • 3.UI設計(QT設計師)
  • 四.源代碼
    • 1.Wei_Bo_pics_Crawl.py(主程式)
    • 2.WeiBo_Pics_Crawl.py(UI)
    • 3.Weibo_Crawl_Engine.py(爬蟲)
  • 五.總結


之前寫過一款python3GUI–微博圖片爬取工具V1.5(附原始碼)
,是基于python自帶的GUI開發包tkinter,實作了對指定用戶圖片的爬取,本次使用PyQt5,遵循UI界面與爬蟲邏輯分離開來,實作思路大體相同,只不過tkinter與QT組件方法有所差別,走起~

一.準備作業

本程式是基于PyQt5的,需要額外安裝一下,這里不多說,
UI界面的設計基于QT設計師,用它設計UI很方便,
關于以上模塊、工具的安裝可以參考PyCharm安裝PyQt5及其工具(Qt Designer、PyUIC、PyRcc)詳細教程

二.預覽

1.啟動

怎么樣,是不是熟悉的感覺
請添加圖片描述

2.搜索

用戶搜索會呼叫兩個API進行檢索,將搜索到的相關用戶去重后,展示在組合框中,
請添加圖片描述

3.開始爬取

選取完圖片保存路徑,點擊開始爬取,程式就開始作業了,爬取狀態會在表格中實時顯示,
請添加圖片描述

4.結果

請添加圖片描述
僅拿一張圖片作為展示,

三.設計流程

1.整體流程

在這里插入圖片描述

2.UI設計(草圖)

在這里插入圖片描述

3.UI設計(QT設計師)

使用QT設計師,設計UI界面,使用QMainWindow作為模板,去除了工具列與狀態欄,保留了選單欄,使用QLabel、QLineEdit、QPushButton、QCombobox、QTableWidget、QGroupBox等組件,整體垂直布局,區域水平布局,
請添加圖片描述

四.源代碼

1.Wei_Bo_pics_Crawl.py(主程式)

#-*-coding:utf-8-*-
import os
import sys
from threading import Thread
from PyQt5.Qt import QMainWindow,QMessageBox,QFileDialog,QMenu,QTableWidgetItem,QApplication
from PyQt5.QtCore import pyqtSignal, pyqtSlot, QSize
from PyQt5.QtGui import QPixmap
from Weibo_Crawl_Engine import Weibo_Pic_Spider
from WeiBo_Pics_Crawl import Ui_MainWindow
"""

圖片可以爬取了,但是圖片爬完的tablewidget顯示還有問題    **已解決**
保存圖片路徑有問題,還要加上用戶名子目錄名稱  **已實作**
1.加載網路圖片
2.自定義信號槽,保證tablewidget在UI執行緒中
"""
class Wei_Bo_Pics_Crawl_Window(QMainWindow):
    single=pyqtSignal(str)
    def __init__(self):
        super().__init__()
        self.s=Weibo_Pic_Spider()
        self.current_path=os.getcwd()
        self.ui=Ui_MainWindow()
        self.ui.setupUi(self)
        self.setFixedSize(self.width(),self.height())
        self.ui.label_show_user_head_img.setMaximumSize(QSize(120,150))
        self.ui.comboBox_users.currentIndexChanged.connect(self.show_user_img)
        self.ui.pushButton_start_crawl.clicked.connect(lambda :self.thread_it(self.do_start_crawl))
        self.ui.action_quit_window.triggered.connect(self.close)
        self.ui.action_do_search.triggered.connect(self.do_search_users)
        self.ui.action_start_crawl.triggered.connect(self.do_start_crawl)
        self.ui.action_do_stop_crawl.triggered.connect(self.do_stop_crawl)
        self.ui.action_open_dir.triggered.connect(self.open_dir)
        self.ui.action_show_about_author.triggered.connect(lambda :QMessageBox.information(self,'關于作者','作者:懷淰メ\nBy:PyQt5'))
        self.ui.tableWidget_show_satauts.customContextMenuRequested.connect(self.show_right_menu)
        self.ui.pushButton_stop_crawl.clicked.connect(self.do_stop_crawl)
        self.single[str].connect(self.insert_into_table_widget)

    def do_search_users(self):
        self.ui.comboBox_users.clear()
        self.s.user_list.clear()
        key_word=self.ui.lineEdit_keyword.text()
        if key_word !='':
            self.search_result=self.s.get_users(key_word)
            if self.search_result:
                user_names=[user_['user_name'] for user_ in self.search_result]
                self.ui.comboBox_users.addItems(user_names)
                self.ui.pushButton_start_crawl.setEnabled(True)
                self.ui.action_start_crawl.setEnabled(True)
            else:
                QMessageBox.information(self,'提示',f'很抱歉,沒有檢索到關于[{key_word}]的用戶!')
        else:
            QMessageBox.warning(self,'警告','關鍵字不能為空!')

    def do_select_save_path(self):
        while True:
            dir_choose = QFileDialog.getExistingDirectory(self,"選擇要視頻圖片的檔案夾",self.current_path)  # 起始路徑
            if dir_choose!='':
                self.ui.lineEdit_save_path.setText(dir_choose)
                break

    def do_start_crawl(self):
        save_path = self.ui.lineEdit_save_path.text()
        if save_path == '':
            QMessageBox.warning(self, '警告', '存盤路徑不能為空!')
        else:
            if os.path.isdir(save_path):
                self.ui.tableWidget_show_satauts.clear()
                self.ui.tableWidget_show_satauts.setRowCount(0)
                self.ui.tableWidget_show_satauts.setHorizontalHeaderLabels(['狀態'])
                combobox_current_index = self.ui.comboBox_users.currentIndex()
                user_name = self.search_result[combobox_current_index]['user_name']
                user_id = self.search_result[combobox_current_index]['user_id']
                self.pic_save_dir = save_path + '/' + user_name
                os.makedirs(self.pic_save_dir,exist_ok=True)
                self.s.set_start_url(user_id=user_id)
                self.ui.pushButton_start_crawl.setEnabled(False)
                self.ui.action_start_crawl.setEnabled(False)
                self.ui.pushButton_stop_crawl.setEnabled(True)
                self.ui.action_do_stop_crawl.setEnabled(True)
                self.running_flag=True
                self.do_download_pics()
                if self.ui.action_open_dir_after_crawl.isChecked():
                    os.startfile(self.pic_save_dir)
                self.ui.pushButton_start_crawl.setEnabled(True)
                self.ui.action_start_crawl.setEnabled(True)
                self.ui.action_do_stop_crawl.setEnabled(False)
            else:
                QMessageBox.warning(self, '警告', '所選目錄不合法!')
                self.ui.lineEdit_save_path.setText('')

    def do_download_pics(self):
        for pic in self.s.get_pics_url():
            if self.running_flag:
                file_name = pic.split('/')[-1]
                self.s.do_download_pic(pic, file_name, self.pic_save_dir)
                self.single.emit(file_name)
            else:
                self.single.emit('程式已停止!')
                break
        if self.running_flag:
            self.single.emit('圖片爬取結束!')

    def show_user_img(self,index):
        current_user_item=self.search_result[index]
        user_header_img_url=current_user_item['user_head_img']
        pix_map=QPixmap()
        header_img_bytes=self.s.get_img_bytes(user_header_img_url)
        pix_map.loadFromData(header_img_bytes)
        self.ui.label_show_user_head_img.setPixmap(pix_map)

    def do_stop_crawl(self):
        self.single.emit('程式正在停止,請耐心等待...')
        self.running_flag=False
        self.ui.pushButton_start_crawl.setEnabled(True)
        self.ui.action_start_crawl.setEnabled(True)
        self.ui.action_do_stop_crawl.setEnabled(False)
        self.ui.pushButton_stop_crawl.setEnabled(False)

    def show_right_menu(self,pos):
        menu=QMenu()
        item=menu.addAction('查看此圖片')
        screen_pos=self.ui.tableWidget_show_satauts.mapToGlobal(pos)
        action = menu.exec(screen_pos)
        if action==item:
            try:
                current_pic_name=self.ui.tableWidget_show_satauts.currentItem().text()
                pic_path=self.pic_save_dir+'/'+current_pic_name
                os.startfile(pic_path)
            except AttributeError:
                QMessageBox.warning(self,'警告','請務必選擇一張圖片!')
            except FileNotFoundError:
                QMessageBox.warning(self,'警告','此為系統訊息!')


    def closeEvent(self,event):
        reply = QMessageBox.question(self, '關閉', "確定要退出嗎?",
                                     QMessageBox.Yes | QMessageBox.No, QMessageBox.Yes)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()

    @pyqtSlot(str)
    def insert_into_table_widget(self,line):
        row_num=self.ui.tableWidget_show_satauts.rowCount()
        self.ui.tableWidget_show_satauts.setRowCount(row_num+1)
        new_table_widget_item=QTableWidgetItem(line)
        self.ui.tableWidget_show_satauts.setItem(row_num,0,new_table_widget_item)
        self.ui.tableWidget_show_satauts.scrollToBottom()

    def open_dir(self):
        try:
            os.startfile(self.save_path)
        except AttributeError:
            QMessageBox.warning(self,'警告','請先開始爬取圖片!')

    def thread_it(self,func,*args):
        t=Thread(target=func,args=args)
        t.setDaemon(True)
        t.start()

if __name__ == '__main__':
    app=QApplication(sys.argv)
    ui=Wei_Bo_Pics_Crawl_Window()
    ui.show()
    sys.exit(app.exec_())

2.WeiBo_Pics_Crawl.py(UI)

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'WeiBo_Pics_Crawl.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.setEnabled(True)
        MainWindow.resize(496, 466)
        MainWindow.setMouseTracking(False)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.centralwidget)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setContentsMargins(10, 3, 10, -1)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.horizontalLayout_2.addWidget(self.label)
        self.lineEdit_keyword = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_keyword.setMinimumSize(QtCore.QSize(227, 0))
        self.lineEdit_keyword.setMaximumSize(QtCore.QSize(227, 24))
        self.lineEdit_keyword.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_keyword.setObjectName("lineEdit_keyword")
        self.horizontalLayout_2.addWidget(self.lineEdit_keyword)
        spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_2.addItem(spacerItem)
        self.pushButton_do_search = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_do_search.setObjectName("pushButton_do_search")
        self.horizontalLayout_2.addWidget(self.pushButton_do_search)
        self.verticalLayout_2.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setContentsMargins(10, -1, 10, -1)
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_3.addWidget(self.label_2)
        self.lineEdit_save_path = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit_save_path.setMinimumSize(QtCore.QSize(227, 0))
        self.lineEdit_save_path.setMaximumSize(QtCore.QSize(227, 24))
        self.lineEdit_save_path.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_save_path.setObjectName("lineEdit_save_path")
        self.horizontalLayout_3.addWidget(self.lineEdit_save_path)
        spacerItem1 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_3.addItem(spacerItem1)
        self.pushButton_select_save_path = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_select_save_path.setObjectName("pushButton_select_save_path")
        self.horizontalLayout_3.addWidget(self.pushButton_select_save_path)
        self.verticalLayout_2.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setContentsMargins(10, -1, 10, -1)
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_4.addWidget(self.label_3)
        self.comboBox_users = QtWidgets.QComboBox(self.centralwidget)
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.comboBox_users.sizePolicy().hasHeightForWidth())
        self.comboBox_users.setSizePolicy(sizePolicy)
        self.comboBox_users.setMinimumSize(QtCore.QSize(227, 0))
        self.comboBox_users.setMaximumSize(QtCore.QSize(227, 24))
        self.comboBox_users.setMaxVisibleItems(30)
        self.comboBox_users.setSizeAdjustPolicy(QtWidgets.QComboBox.AdjustToContents)
        self.comboBox_users.setObjectName("comboBox_users")
        self.horizontalLayout_4.addWidget(self.comboBox_users)
        spacerItem2 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_4.addItem(spacerItem2)
        self.pushButton_start_crawl = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_start_crawl.setEnabled(False)
        self.pushButton_start_crawl.setMaximumSize(QtCore.QSize(93, 16777215))
        self.pushButton_start_crawl.setObjectName("pushButton_start_crawl")
        self.horizontalLayout_4.addWidget(self.pushButton_start_crawl)
        self.verticalLayout_2.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_9 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_9.setContentsMargins(-1, -1, 10, -1)
        self.horizontalLayout_9.setObjectName("horizontalLayout_9")
        spacerItem3 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_9.addItem(spacerItem3)
        self.pushButton_stop_crawl = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_stop_crawl.setEnabled(False)
        self.pushButton_stop_crawl.setMaximumSize(QtCore.QSize(93, 16777215))
        self.pushButton_stop_crawl.setObjectName("pushButton_stop_crawl")
        self.horizontalLayout_9.addWidget(self.pushButton_stop_crawl)
        self.verticalLayout_2.addLayout(self.horizontalLayout_9)
        self.horizontalLayout_7 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_7.setSizeConstraint(QtWidgets.QLayout.SetDefaultConstraint)
        self.horizontalLayout_7.setContentsMargins(-1, -1, 10, -1)
        self.horizontalLayout_7.setObjectName("horizontalLayout_7")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_5.setContentsMargins(10, -1, 0, -1)
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setObjectName("label_4")
        self.horizontalLayout_5.addWidget(self.label_4)
        self.tableWidget_show_satauts = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget_show_satauts.setMinimumSize(QtCore.QSize(227, 0))
        font = QtGui.QFont()
        font.setFamily("微軟雅黑")
        font.setPointSize(7)
        self.tableWidget_show_satauts.setFont(font)
        self.tableWidget_show_satauts.setContextMenuPolicy(QtCore.Qt.CustomContextMenu)
        self.tableWidget_show_satauts.setAlternatingRowColors(True)
        self.tableWidget_show_satauts.setTextElideMode(QtCore.Qt.ElideMiddle)
        self.tableWidget_show_satauts.setVerticalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
        self.tableWidget_show_satauts.setHorizontalScrollMode(QtWidgets.QAbstractItemView.ScrollPerPixel)
        self.tableWidget_show_satauts.setObjectName("tableWidget_show_satauts")
        self.tableWidget_show_satauts.setColumnCount(1)
        self.tableWidget_show_satauts.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        self.tableWidget_show_satauts.setHorizontalHeaderItem(0, item)
        self.tableWidget_show_satauts.horizontalHeader().setStretchLastSection(True)
        self.tableWidget_show_satauts.verticalHeader().setDefaultSectionSize(20)
        self.tableWidget_show_satauts.verticalHeader().setMinimumSectionSize(18)
        self.horizontalLayout_5.addWidget(self.tableWidget_show_satauts)
        spacerItem4 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_5.addItem(spacerItem4)
        self.horizontalLayout_6 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_6.setContentsMargins(0, -1, -1, -1)
        self.horizontalLayout_6.setSpacing(0)
        self.horizontalLayout_6.setObjectName("horizontalLayout_6")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setObjectName("groupBox")
        self.verticalLayout = QtWidgets.QVBoxLayout(self.groupBox)
        self.verticalLayout.setObjectName("verticalLayout")
        self.label_show_user_head_img = QtWidgets.QLabel(self.groupBox)
        self.label_show_user_head_img.setMinimumSize(QtCore.QSize(93, 0))
        self.label_show_user_head_img.setAutoFillBackground(False)
        self.label_show_user_head_img.setStyleSheet("QLabel{\n"
"background-color:rgb(173, 216, 230);\n"
"position:absolute;\n"
"bottom:1;\n"
"}")
        self.label_show_user_head_img.setText("")
        self.label_show_user_head_img.setTextFormat(QtCore.Qt.AutoText)
        self.label_show_user_head_img.setScaledContents(True)
        self.label_show_user_head_img.setAlignment(QtCore.Qt.AlignHCenter|QtCore.Qt.AlignTop)
        self.label_show_user_head_img.setObjectName("label_show_user_head_img")
        self.verticalLayout.addWidget(self.label_show_user_head_img)
        self.horizontalLayout_6.addWidget(self.groupBox)
        self.horizontalLayout_5.addLayout(self.horizontalLayout_6)
        self.horizontalLayout_7.addLayout(self.horizontalLayout_5)
        self.verticalLayout_2.addLayout(self.horizontalLayout_7)
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.frame = QtWidgets.QFrame(self.centralwidget)
        self.frame.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.frame.setStyleSheet("color: rgb(255, 0, 0);")
        self.frame.setFrameShape(QtWidgets.QFrame.Box)
        self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
        self.frame.setObjectName("frame")
        self.horizontalLayout_8 = QtWidgets.QHBoxLayout(self.frame)
        self.horizontalLayout_8.setContentsMargins(10, -1, 10, -1)
        self.horizontalLayout_8.setObjectName("horizontalLayout_8")
        spacerItem5 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_8.addItem(spacerItem5)
        spacerItem6 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_8.addItem(spacerItem6)
        self.label_6 = QtWidgets.QLabel(self.frame)
        self.label_6.setAlignment(QtCore.Qt.AlignCenter)
        self.label_6.setObjectName("label_6")
        self.horizontalLayout_8.addWidget(self.label_6)
        spacerItem7 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_8.addItem(spacerItem7)
        spacerItem8 = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.horizontalLayout_8.addItem(spacerItem8)
        self.horizontalLayout.addWidget(self.frame)
        self.verticalLayout_2.addLayout(self.horizontalLayout)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 496, 26))
        self.menubar.setObjectName("menubar")
        self.menu = QtWidgets.QMenu(self.menubar)
        self.menu.setObjectName("menu")
        self.menu_2 = QtWidgets.QMenu(self.menubar)
        self.menu_2.setObjectName("menu_2")
        self.menu_3 = QtWidgets.QMenu(self.menubar)
        self.menu_3.setObjectName("menu_3")
        MainWindow.setMenuBar(self.menubar)
        self.action_open_dir = QtWidgets.QAction(MainWindow)
        self.action_open_dir.setObjectName("action_open_dir")
        self.action_quit_window = QtWidgets.QAction(MainWindow)
        self.action_quit_window.setObjectName("action_quit_window")
        self.action_do_search = QtWidgets.QAction(MainWindow)
        self.action_do_search.setObjectName("action_do_search")
        self.action_start_crawl = QtWidgets.QAction(MainWindow)
        self.action_start_crawl.setEnabled(False)
        self.action_start_crawl.setObjectName("action_start_crawl")
        self.action_open_dir_after_crawl = QtWidgets.QAction(MainWindow)
        self.action_open_dir_after_crawl.setCheckable(True)
        self.action_open_dir_after_crawl.setObjectName("action_open_dir_after_crawl")
        self.action_show_about_author = QtWidgets.QAction(MainWindow)
        self.action_show_about_author.setObjectName("action_show_about_author")
        self.action_do_stop_crawl = QtWidgets.QAction(MainWindow)
        self.action_do_stop_crawl.setEnabled(False)
        self.action_do_stop_crawl.setObjectName("action_do_stop_crawl")
        self.menu.addAction(self.action_open_dir)
        self.menu.addSeparator()
        self.menu.addAction(self.action_quit_window)
        self.menu_2.addAction(self.action_do_search)
        self.menu_2.addAction(self.action_start_crawl)
        self.menu_2.addAction(self.action_do_stop_crawl)
        self.menu_2.addSeparator()
        self.menu_2.addAction(self.action_open_dir_after_crawl)
        self.menu_3.addAction(self.action_show_about_author)
        self.menubar.addAction(self.menu.menuAction())
        self.menubar.addAction(self.menu_2.menuAction())
        self.menubar.addAction(self.menu_3.menuAction())

        self.retranslateUi(MainWindow)
        self.pushButton_do_search.clicked.connect(MainWindow.do_search_users)
        self.pushButton_select_save_path.clicked.connect(MainWindow.do_select_save_path)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "微博圖片小爬蟲-V1.5"))
        self.label.setText(_translate("MainWindow", "關鍵字:  "))
        self.lineEdit_keyword.setPlaceholderText(_translate("MainWindow", "請輸入關鍵字"))
        self.pushButton_do_search.setText(_translate("MainWindow", "搜索"))
        self.pushButton_do_search.setShortcut(_translate("MainWindow", "Return"))
        self.label_2.setText(_translate("MainWindow", "保存位置:"))
        self.lineEdit_save_path.setPlaceholderText(_translate("MainWindow", "請選擇路徑"))
        self.pushButton_select_save_path.setText(_translate("MainWindow", "選擇"))
        self.pushButton_select_save_path.setShortcut(_translate("MainWindow", "Ctrl+Shift+D"))
        self.label_3.setText(_translate("MainWindow", "用戶串列:"))
        self.pushButton_start_crawl.setText(_translate("MainWindow", "開始爬取"))
        self.pushButton_start_crawl.setShortcut(_translate("MainWindow", "Ctrl+Shift+S"))
        self.pushButton_stop_crawl.setText(_translate("MainWindow", "停止爬取"))
        self.label_4.setText(_translate("MainWindow", "當前狀態:"))
        item = self.tableWidget_show_satauts.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "狀態"))
        self.groupBox.setTitle(_translate("MainWindow", "用戶頭像"))
        self.label_6.setText(_translate("MainWindow", "敬告:本軟體僅供學習交流使用"))
        self.menu.setTitle(_translate("MainWindow", "開始"))
        self.menu_2.setTitle(_translate("MainWindow", "操作"))
        self.menu_3.setTitle(_translate("MainWindow", "關于"))
        self.action_open_dir.setText(_translate("MainWindow", "打開檔案夾"))
        self.action_open_dir.setShortcut(_translate("MainWindow", "Ctrl+O"))
        self.action_quit_window.setText(_translate("MainWindow", "退出"))
        self.action_quit_window.setShortcut(_translate("MainWindow", "Ctrl+Q"))
        self.action_do_search.setText(_translate("MainWindow", "搜索"))
        self.action_do_search.setShortcut(_translate("MainWindow", "Ctrl+Return"))
        self.action_start_crawl.setText(_translate("MainWindow", "開始爬取"))
        self.action_start_crawl.setShortcut(_translate("MainWindow", "Ctrl+Shift+S"))
        self.action_open_dir_after_crawl.setText(_translate("MainWindow", "爬取完成后打開檔案夾"))
        self.action_show_about_author.setText(_translate("MainWindow", "關于作者"))
        self.action_show_about_author.setShortcut(_translate("MainWindow", "Ctrl+B"))
        self.action_do_stop_crawl.setText(_translate("MainWindow", "停止爬取"))
        self.action_do_stop_crawl.setShortcut(_translate("MainWindow", "Ctrl+Shift+B"))

3.Weibo_Crawl_Engine.py(爬蟲)

由于篇幅,Weibo_Crawl_Engine.py的代碼詳見:

python3GUI–微博圖片爬取工具V1.5(附原始碼)

五.總結

本次使用PyQt5撰寫一款微博圖片爬取工具,在爬取程序中遇到了兩個難點問題:
1.Qlabel顯示網路圖片
此問題解決參考了pyqt5加載網路圖片,不本地下載,
2.自定義信號槽,保證tablewidget在UI執行緒中
此問題解決參考了PyQt 5信號與槽的幾種高級玩法
感謝各位!程式打包好放在了百度云 密碼:8888,思路、代碼方面有什么不足歡迎各位大佬指正、批評!

請添加圖片描述

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/302793.html

標籤:python

上一篇:引入具有爭議的 Idle Detection API,Chrome 94 發布

下一篇:selenium運行常見的報錯問題--讓你及時發現問題的所在

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • 【C++】Microsoft C++、C 和匯編程式檔案

    ......

    uj5u.com 2020-09-10 00:57:23 more
  • 例外宣告

    相比于斷言適用于排除邏輯上不可能存在的狀態,例外通常是用于邏輯上可能發生的錯誤。 例外宣告 Item 1:當函式不可能拋出例外或不能接受拋出例外時,使用noexcept 理由 如果不打算拋出例外的話,程式就會認為無法處理這種錯誤,并且應當盡早終止,如此可以有效地阻止例外的傳播與擴散。 示例 //不可 ......

    uj5u.com 2020-09-10 00:57:27 more
  • Codeforces 1400E Clear the Multiset(貪心 + 分治)

    鏈接:https://codeforces.com/problemset/problem/1400/E 來源:Codeforces 思路:給你一個陣列,現在你可以進行兩種操作,操作1:將一段沒有 0 的區間進行減一的操作,操作2:將 i 位置上的元素歸零。最終問:將這個陣列的全部元素歸零后操作的最少 ......

    uj5u.com 2020-09-10 00:57:30 more
  • UVA11610 【Reverse Prime】

    本人看到此題沒有翻譯,就附帶了一個自己的翻譯版本 思考 這一題,它的第一個要求是找出所有 $7$ 位反向質數及其質因數的個數。 我們應該需要質數篩篩選1~$10^{7}$的所有數,這里就不慢慢介紹了。但是,重讀題,我們突然發現反向質數都是 $7$ 位,而將它反過來后的數字卻是 $6$ 位數,這就說明 ......

    uj5u.com 2020-09-10 00:57:36 more
  • 統計區間素數數量

    1 #pragma GCC optimize(2) 2 #include <bits/stdc++.h> 3 using namespace std; 4 bool isprime[1000000010]; 5 vector<int> prime; 6 inline int getlist(int ......

    uj5u.com 2020-09-10 00:57:47 more
  • C/C++編程筆記:C++中的 const 變數詳解,教你正確認識const用法

    1、C中的const 1、區域const變數存放在堆疊區中,會分配記憶體(也就是說可以通過地址間接修改變數的值)。測驗代碼如下: 運行結果: 2、全域const變數存放在只讀資料段(不能通過地址修改,會發生寫入錯誤), 默認為外部聯編,可以給其他源檔案使用(需要用extern關鍵字修飾) 運行結果: ......

    uj5u.com 2020-09-10 00:58:04 more
  • 【C++犯錯記錄】VS2019 MFC添加資源不懂如何修改資源宏ID

    1. 首先在資源視圖中,添加資源 2. 點擊新添加的資源,復制自動生成的ID 3. 在解決方案資源管理器中找到Resource.h檔案,編輯,使用整個專案搜索和替換的方式快速替換 宏宣告 4. Ctrl+Shift+F 全域搜索,點擊查找全部,然后逐個替換 5. 為什么使用搜索替換而不使用屬性視窗直 ......

    uj5u.com 2020-09-10 00:59:11 more
  • 【C++犯錯記錄】VS2019 MFC不懂的批量添加資源

    1. 打開資源頭檔案Resource.h,在其中預先定義好宏 ID(不清楚其實ID值應該設定多少,可以先新建一個相同的資源項,再在這個資源的ID值的基礎上遞增即可) 2. 在資源視圖中選中專案資源,按F7編輯資源檔案,按 ID 型別 相對路徑的形式添加 資源。(別忘了先把檔案拷貝到專案中的res檔案 ......

    uj5u.com 2020-09-10 01:00:19 more
  • C/C++編程筆記:關于C++的參考型別,專供新手入門使用

    今天要講的是C++中我最喜歡的一個用法——參考,也叫別名。 參考就是給一個變數名取一個變數名,方便我們間接地使用這個變數。我們可以給一個變數創建N個參考,這N + 1個變數共享了同一塊記憶體區域。(參考型別的變數會占用記憶體空間,占用的記憶體空間的大小和指標型別的大小是相同的。雖然參考是一個物件的別名,但 ......

    uj5u.com 2020-09-10 01:00:22 more
  • 【C/C++編程筆記】從頭開始學習C ++:初學者完整指南

    眾所周知,C ++的學習曲線陡峭,但是花時間學習這種語言將為您的職業帶來奇跡,并使您與其他開發人員區分開。您會更輕松地學習新語言,形成真正的解決問題的技能,并在編程的基礎上打下堅實的基礎。 C ++將幫助您養成良好的編程習慣(即清晰一致的編碼風格,在撰寫代碼時注釋代碼,并限制類內部的可見性),并且由 ......

    uj5u.com 2020-09-10 01:00:41 more
最新发布
  • Rust中的智能指標:Box<T> Rc<T> Arc<T> Cell<T> RefCell<T> Weak

    Rust中的智能指標是什么 智能指標(smart pointers)是一類資料結構,是擁有資料所有權和額外功能的指標。是指標的進一步發展 指標(pointer)是一個包含記憶體地址的變數的通用概念。這個地址參考,或 ” 指向”(points at)一些其 他資料 。參考以 & 符號為標志并借用了他們所 ......

    uj5u.com 2023-04-20 07:24:10 more
  • Java的值傳遞和參考傳遞

    值傳遞不會改變本身,參考傳遞(如果傳遞的值需要實體化到堆里)如果發生修改了會改變本身。 1.基本資料型別都是值傳遞 package com.example.basic; public class Test { public static void main(String[] args) { int ......

    uj5u.com 2023-04-20 07:24:04 more
  • [2]SpinalHDL教程——Scala簡單入門

    第一個 Scala 程式 shell里面輸入 $ scala scala> 1 + 1 res0: Int = 2 scala> println("Hello World!") Hello World! 檔案形式 object HelloWorld { /* 這是我的第一個 Scala 程式 * 以 ......

    uj5u.com 2023-04-20 07:23:58 more
  • 理解函式指標和回呼函式

    理解 函式指標 指向函式的指標。比如: 理解函式指標的偽代碼 void (*p)(int type, char *data); // 定義一個函式指標p void func(int type, char *data); // 宣告一個函式func p = func; // 將指標p指向函式func ......

    uj5u.com 2023-04-20 07:23:52 more
  • Django筆記二十五之資料庫函式之日期函式

    本文首發于公眾號:Hunter后端 原文鏈接:Django筆記二十五之資料庫函式之日期函式 日期函式主要介紹兩個大類,Extract() 和 Trunc() Extract() 函式作用是提取日期,比如我們可以提取一個日期欄位的年份,月份,日等資料 Trunc() 的作用則是截取,比如 2022-0 ......

    uj5u.com 2023-04-20 07:23:45 more
  • 一天吃透JVM面試八股文

    什么是JVM? JVM,全稱Java Virtual Machine(Java虛擬機),是通過在實際的計算機上仿真模擬各種計算機功能來實作的。由一套位元組碼指令集、一組暫存器、一個堆疊、一個垃圾回收堆和一個存盤方法域等組成。JVM屏蔽了與作業系統平臺相關的資訊,使得Java程式只需要生成在Java虛擬機 ......

    uj5u.com 2023-04-20 07:23:31 more
  • 使用Java接入小程式訂閱訊息!

    更新完微信服務號的模板訊息之后,我又趕緊把微信小程式的訂閱訊息給實作了!之前我一直以為微信小程式也是要企業才能申請,沒想到小程式個人就能申請。 訊息推送平臺🔥推送下發【郵件】【短信】【微信服務號】【微信小程式】【企業微信】【釘釘】等訊息型別。 https://gitee.com/zhongfuch ......

    uj5u.com 2023-04-20 07:22:59 more
  • java -- 緩沖流、轉換流、序列化流

    緩沖流 緩沖流, 也叫高效流, 按照資料型別分類: 位元組緩沖流:BufferedInputStream,BufferedOutputStream 字符緩沖流:BufferedReader,BufferedWriter 緩沖流的基本原理,是在創建流物件時,會創建一個內置的默認大小的緩沖區陣列,通過緩沖 ......

    uj5u.com 2023-04-20 07:22:49 more
  • Java-SpringBoot-Range請求頭設定實作視頻分段傳輸

    老實說,人太懶了,現在基本都不喜歡寫筆記了,但是網上有關Range請求頭的文章都太水了 下面是抄的一段StackOverflow的代碼...自己大修改過的,寫的注釋挺全的,應該直接看得懂,就不解釋了 寫的不好...只是希望能給視頻網站開發的新手一點點幫助吧. 業務場景:視頻分段傳輸、視頻多段傳輸(理 ......

    uj5u.com 2023-04-20 07:22:42 more
  • Windows 10開發教程_編程入門自學教程_菜鳥教程-免費教程分享

    教程簡介 Windows 10開發入門教程 - 從簡單的步驟了解Windows 10開發,從基本到高級概念,包括簡介,UWP,第一個應用程式,商店,XAML控制元件,資料系結,XAML性能,自適應設計,自適應UI,自適應代碼,檔案管理,SQLite資料庫,應用程式到應用程式通信,應用程式本地化,應用程式 ......

    uj5u.com 2023-04-20 07:22:35 more