PyQt5 GUI: Baidu picture downloader (source code attached at the end of the text)

Posted by markc1 on Wed, 22 Dec 2021 09:38:36 +0100

An interface Downloader is implemented through Pyqt5 to download various types of pictures through network request. You can enter the keywords of different pictures on the interface to download the pictures and save the downloaded pictures to the user-defined file path.

The source code and operation video are attached at the end of the paper

[read the full text]

Before introducing the code block, let's take a look at the three-party python libraries that need to be used.

from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import os

from scripy_images import ScripyImages

The three modules imported from Pyqt5 have been mentioned in the introduction of the weather query system before. If you need to download the source code of Pyqt5 weather query system, please reply in the official account: "weather query system". The remaining two operation libraries are os module, which is mainly used for system file related operations, and scripy_ The images module is used to obtain and download images through network requests.

Next, we introduce the use of Pyqt5 related page definitions and other functions. Similarly, first add the required components on the Pyqt5 window, and finally add these components to the layout. The general implementation process follows this order.

  def init_ui(self):
        self.setWindowTitle('Baidu image extraction application')

        grid = QGridLayout()

        self.page_label = QLabel()
        self.page_label.setText('Set the number of pages to crawl:')
        self.page_line_text = QLineEdit()
        self.page_line_text.setPlaceholderText('Enter integer')
        self.page_line_text.setValidator(QIntValidator(1, 99))
        self.page_line_text.setFocus()

        self.keyword_label = QLabel()
        self.keyword_label.setText('Set diagram Keywords:')
        self.keyword_line_text = QLineEdit()
        self.keyword_line_text.setValidator(QRegExpValidator(QRegExp('[\u4E00-\u9FA5]+')))
        self.keyword_line_text.setMaxLength(6)
        self.keyword_line_text.setPlaceholderText('Input Chinese characters')

        self.file_path = QLineEdit()
        self.file_path.setPlaceholderText('Custom file path')
        self.file_path.setReadOnly(True)
        self.file_path_button = QPushButton()
        self.file_path_button.setText('Custom path')
        self.file_path_button.clicked.connect(self.file_path_click)

        self.request_button = QPushButton()
        self.request_button.setText('Quickly start capturing pictures')
        self.request_button.clicked.connect(self.download_image)

        self.log_text = QTextEdit()
        self.log_text.setPlaceholderText('Capture progress result display...')
        self.log_text.setReadOnly(True)
        self.log_text.setMaximumHeight(100)

        self.version_msg_label = QLabel()
        self.version_msg_label.setText('<font color=blue>Official account:[Python concentration camp] release</font>')
        self.version_msg_label.setAlignment(Qt.AlignCenter)

        grid.addWidget(self.page_label, 0, 0, 1, 1)
        grid.addWidget(self.page_line_text, 0, 1, 1, 2)
        grid.addWidget(self.keyword_label, 1, 0, 1, 1)
        grid.addWidget(self.keyword_line_text, 1, 1, 1, 2)
        grid.addWidget(self.file_path, 2, 0, 1, 2)
        grid.addWidget(self.file_path_button, 2, 2, 1, 1)
        grid.addWidget(self.request_button, 3, 0, 1, 3)
        grid.addWidget(self.log_text, 4, 0, 1, 3)
        grid.addWidget(self.version_msg_label, 5, 0, 1, 3)

        self.setLayout(grid)

Then, define the corresponding slot function. One of the two slot functions is to actually define the storage path of the file. When you need a slot function to obtain the file path. Another is to start the download process of Baidu pictures, and call the execution of the download module through this slot function.

   def file_path_click(self):
        self.cwd = os.getcwd()
        directory = QFileDialog.getExistingDirectory(self, 'Select Folder', self.cwd)
        print(directory)
        self.file_path.setText(directory + '/')

    def download_image(self):
        check_param = False
        self.log_text.setText("")
        self.log_text.insertPlainText("-----Start mandatory parameter check-----\n")
        if self.page_line_text.text().strip() != '' and \
                self.keyword_line_text.text().strip() != '' and \
                self.file_path.text().strip() != '':
            self.log_text.insertPlainText("---Parameter check succeeded---\n")
            check_param = True
        else:
            self.log_text.insertPlainText("---Parameter check failed---\n")
            self.log_text.insertPlainText("Please fill in the required fields before continuing...\n")
            check_param = False
        self.log_text.insertPlainText("-----End mandatory parameter check-----\n")

        if check_param is True:
            self.log_text.insertPlainText("-----Start downloading Baidu pictures-----\n")
            self.log_text.insertPlainText("---Please wait patiently---\n")
            ScripyImages(page_num=self.page_line_text.text(),current=self.keyword_line_text.text(),file_path=self.file_path.text())
            self.log_text.insertPlainText("-----End downloading Baidu pictures-----\n")

Then it calls the main function to execute the whole logic.

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

The whole Pyqt5 calls execution process is like this, get the complete block of code, including the Baidu picture execution network request download part, in the official account reply to Baidu image download device to get the complete code block.

Operation video demonstration

[review of previous periods]

Flexible conversion between zip(), zip(*) and list() in Python 3!

Formatted string output of python print() function

Pyqt5 GUI & & requests API make a weather query system (get the complete code at the end of the text)!

A beautiful windows cmd command line tool cmder

How to write and save visual data after excel data analysis!

excel data processing II: quickly complete the addition and modification of openpyxl data!

excel data processing 1: skillfully using openpyxl to extract and filter data

Helium, a more convenient automated testing tool than Selenium!

Python data visualization: visual data analysis plug-in D-Tale

Too slow to calculate? Try lru_cache decorator!

Topics: Python