Baidu picture downloader 2.0

Posted by Dr John on Thu, 27 Jan 2022 03:16:31 +0100

I wrote a Baidu picture downloader some time ago, and found that many people need to use it. To tell you the truth, the baidu picture downloader written before is relatively LOW. Today, I just had time to upgrade it.

The way to obtain the complete source code is at the end of the article. If necessary, you can download it directly.

Two bugs have been updated. One is that when there are thousands of pictures to download, there are no pictures to download. The other is that the download progress cannot be displayed in real time. I don't know how far the download has reached.

Similarly, we first import the required third-party libraries.

'''UI Interface related libraries'''

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

'''Libraries related to application operations'''
import sys
import os

from scripy_images import ScripyImages

On the basis of the original UI interface, a text browser is added to view the download progress in real time, and the control of the number of pictures downloaded on each page is increased to make the download data more accurate.

 def init_ui(self):
        self.setWindowTitle('Baidu picture downloader 2.0  Official account:[Python concentration camp]')
        self.setWindowIcon(QIcon('download.ico'))
        self.setFixedSize(550, 300)

        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.page_num_label = QLabel()
        self.page_num_label.setText('Number of crawls per page:')
        self.page_num_text = QSpinBox()
        self.page_num_text.setRange(50, 100)
        self.page_num_text.setSingleStep(10)
        self.page_num_text.setWrapping(True)

        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.brower = QTextBrowser()
        self.brower.setPlaceholderText('Capture progress result display...')

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

        self.thread_ = ScripyImages(self)
        self.thread_.trigger.connect(self.update_log)
        self.thread_.finished.connect(self.finished)

        self.setLayout(grid)

Add a slot function to control the real-time writing of data to the text browser.

  def update_log(self, text):
        '''
        Slot function: write content to text browser
        :param text:
        :return:
        '''
        cursor = self.brower.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.brower.append(text)
        self.brower.setTextCursor(cursor)
        self.brower.ensureCursorVisible()

It optimizes the image download process and uses special sub threads to process the image download part.

  def download_image(self):
        self.request_button.setEnabled(False)
        self.thread_.start()

The thread processing logic of image download can be downloaded in the complete source code. The code block is not shown here.

Reply to the Baidu image download 2 in the official account to get the complete source code.

I am a [Python camp. I am glad you saw it. Finally, I am a official account focused on Python knowledge sharing. I hope you can get your attention.

[previous highlights]

gif dynamic picture generator, multiple pictures are combined to generate dynamic pictures

python several common data processing operations, one line of code can be completed!

Chinese new year, use PyQt5 to generate a pair of Spring Festival couplets

Record the formatting corresponding to the top ten% placeholders in python

PyQt5 UI makes a Douban movie information viewer and first knows QThread multithreading

Topics: Python