µ±Ç°Î»ÖãºÊ×Ò³ > ¿ª·¢½Ì³Ì > ajax½Ì³Ì >

»ùÓÚPyQt5ÖÆ×÷Ò»¸ö±íÇé°üÏÂÔØÆ÷

ʱ¼ä£º2022-03-29 11:11 À´Ô´£ºÎ´Öª ×÷ÕߣºÍæÈ˱ر»ÈËÍæ ÊÕ²Ø

ÿ´ÎºÍÅóÓÑÁÄÌì¿àÓÚûÓбíÇé°ü£¬¶ø±ðÈ˵ıíÇé°üËƺõÊÇÈ¡Ö®²»¾¡¡¢ÓÃÖ®²»½ß¡£×÷Ϊһ¸ö³ÌÐòÔ±ÄÄÄܸÊÔ¸ÈÏÊ䣬ËùÒÔ±¾ÎĽ«ÓÃPythonÖÆ×÷Ò»¸ö±íÇé°üÏÂÔØÆ÷¹©´ó¼Ò¶·Í¼£¬ÐèÒªµÄ¿ÉÒԲο¼Ò»ÏÂ

ÿ´ÎºÍÅóÓÑÁÄÌì¿àÓÚûÓбíÇé°ü£¬¶ø±ðÈ˵ıíÇé°üËƺõÊÇÈ¡Ö®²»¾¡¡¢ÓÃÖ®²»½ß¡£×÷Ϊһ¸ö³ÌÐòÔ±ÄÄÄܸÊÔ¸ÈÏÊ䣬ÓÚÊÇ×öÁËÒ»¸ö±íÇé°üÏÂÔØÆ÷¹©´ó¼Ò¶·Í¼¡£

Ê×ÏÈ£¬»¹ÊǽéÉÜÒ»ÏÂÉè¼Æ˼·°É£¬ºÍÎÒÃÇ֮ǰ×öµÄ°Ù¶ÈͼƬÏÂÔØÆ÷2.0Ò»Ñù£¬Ê¹ÓÃpyqt5×÷ΪUI½çÃæÖÆ×÷µÄ¿ò¼Ü£¬È»ºó¾ÍÊÇÕÒÒ»¸ö±íÇé°üÍøÕ¾¹©ÎÒÃÇ¿ÉÒÔÏÂÔغܶàµÄ±íÇé°ü¡£

±íÇé°üʹÓõÄÍøÕ¾ÊÇÕâ¸ö£¬´ó¼ÒÒ²¿ÉÒÔʹÓÃ×Ô¼º·¢ÏֵıíÇé°üÍøÕ¾×öÏÂÔØ¡£

»°²»¶à˵£¬ÎÒÃÇÏÈ˵Ã÷Ò»ÏÂʹÓõ½µÄpython¿âÓÐÄÄЩ¡£

UI½çÃæʹÓõ½µÄpyqt5Ä£¿éÊÇÏÂÃæÕ⼸¸ö£¬Ö®Ç°Ò²ÊÇһֱʹÓÃÕ⼸¸ö¿â×öUI½çÃ濪·¢µÄ¡£

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

ÏÂÃæÊÇÔÚÏÂÔØ£¨Ò²¿ÉÒÔ˵ÊÇÅÀ³æ£©±íÇé°üʱʹÓõ½µÄpythonÓ¦Óÿ⡣

import requests
import re
from urllib.request import urlretrieve
from fake_useragent import UserAgent

½«Ê¹Óõ½µÄ±ê×¼»ò·Ç±ê×¼¿â×¼±¸ºÃ£¬ÏÈÀ´±àдUI½çÃ棬ÏÂÃæ¾Í¿ªÊ¼ÎÒÃǵıíÑÝÁË¡£

class Emoji(QWidget):
    def __init__(self):
        super(Emoji, self).__init__()
        self.init_ui()

    def init_ui(self):
        '''
        ³õʼ»¯UI½çÃæ²¼¾Ö
        :return:
        '''
        self.setWindowTitle('±íÇé°üÏÂÔØÆ÷   ¹«Öںţº[Python ¼¯ÖÐÓª]')
        self.setWindowIcon(QIcon('±íÇé°üͼ±ê.png'))
        self.setFixedSize(500, 300)

        grid = QGridLayout()

        self.page_size = QLabel()
        self.page_size.setText('ĬÈÏÿҳÊýÁ¿£º')

        self.page_size_text = QLineEdit()
        self.page_size_text.setText('45')
        self.page_size_text.setReadOnly(True)

        self.page_num = QLabel()
        self.page_num.setText('ÉèÖÃÏÂÔØÒ³Êý£º')

        self.page_num_text = QLineEdit()
        self.page_num_text.setPlaceholderText('ÇëÊäÈëÕûÊý 1~200')
        self.page_num_text.setValidator(QIntValidator(1, 200))

        self.save_dir = QLineEdit()
        self.save_dir.setReadOnly(True)
        self.save_dir.setPlaceholderText('ͼƬ´æ´¢Â·¾¶')

        self.save_dir_btn = QPushButton()
        self.save_dir_btn.setText('ÉèÖô洢·¾¶')
        self.save_dir_btn.clicked.connect(self.save_dir_btn_click)

        self.brower = QTextBrowser()
        self.brower.setPlaceholderText('ÏÂÔؽø¶È½á¹ûչʾÇøÓò...')

        self.start_btn = QPushButton()
        self.start_btn.setText('¿ªÊ¼ÏÂÔرíÇé°ü')
        self.start_btn.clicked.connect(self.start_btn_click)

        grid.addWidget(self.page_size, 0, 0, 1, 1)
        grid.addWidget(self.page_size_text, 0, 1, 1, 1)
        grid.addWidget(self.page_num, 1, 0, 1, 1)
        grid.addWidget(self.page_num_text, 1, 1, 1, 1)
        grid.addWidget(self.save_dir, 2, 0, 1, 1)
        grid.addWidget(self.save_dir_btn, 2, 1, 1, 1)
        grid.addWidget(self.brower, 3, 0, 1, 2)
        grid.addWidget(self.start_btn, 4, 0, 1, 2)

        self.thread_ = DownloadThread(self)
        self.thread_.finished.connect(self.finished)
        self.thread_.log.connect(self.set_log)

        self.setLayout(grid)

    def save_dir_btn_click(self):
        '''
        ÉèÖô洢Îļþ·¾¶
        :return:
        '''
        dir = QFileDialog.getExistingDirectory(self, "Ñ¡ÔñÎļþ¼Ð", os.getcwd())
        self.save_dir.setText(dir)

    def start_btn_click(self):
        '''
        Æô¶¯×ÓÏß³ÌÏÂÔرíÇé°ü
        :return:
        '''
        self.start_btn.setEnabled(False)
        self.thread_.start()
        self.set_log('ÏÂÔØÏß³ÌÒѾ­Æô¶¯...')

    def set_log(self, text):
        '''
        ¸üÐÂÎı¾ä¯ÀÀÆ÷ÄÚÈÕÖ¾ÐÅÏ¢
        :param text:
        :return:
        '''
        cursor = self.brower.textCursor()
        cursor.movePosition(QTextCursor.End)
        self.brower.append(text)
        self.brower.setTextCursor(cursor)
        self.brower.ensureCursorVisible()

    def finished(self, finished):
        if finished is True:
            self.start_btn.setEnabled(True)

±àдÍêUI½çÃæÒÔºó¿ªÊ¼±àдÓÃÓÚÏÂÔرíÇé°üµÄ×ÓḬ̈߳ɣ¬×ÓÏ̱߳àдÐèÒª¼Ì³Ðpyqt5ÖеÄQThreadÏß³ÌÀ´±àд¡£

class DownloadThread(QThread):
    finished = pyqtSignal(bool)
    log = pyqtSignal(str)

    def __init__(self, parent=None):
        super(DownloadThread, self).__init__(parent)
        self.parent = parent
        self.working = True

    def __del__(self):
        self.working = False
        self.wait()

    def run(self):
        self.download()

    def download(self):
        user_agent = UserAgent()
        page_num = int(self.parent.page_num_text.text())
        save_dir = self.parent.save_dir.text()
        for n in range(1, page_num):
            url = 'https://www.fabiaoqing.com/biaoqing/lists/page/{}.html'.format(n)
            headers = {
                'user-agent': user_agent.random
            }
            response = requests.get(url, headers=headers)
            repx = re.compile('data-original="(.*?)" title="(.*?)"', re.I)
            texts = repx.findall(response.text)
            for text in texts:
                emoji_url = text[0].split('" src="')[0]
                emoji_name = emoji_url.split('/')[-1]
                urlretrieve(emoji_url,
                            save_dir + '/' + emoji_name)
                self.log.emit(emoji_name + ' ÏÂÔØÍê³É£¡')
        self.log.emit('×ÓÏß³ÌÏÂÔØÍê³É£¡')
        self.finished.emit(True)

×îºó£¬Ê¹ÓÃmainº¯Êý½«Ò³Ãæ²¼¾Ö¼ÓÈëµ½Ó¦ÓõÄÖ÷ÌåÑ­»·µ±ÖУ¬Æô¶¯Õû¸öÓ¦Óþʹ󹦸æ³ÉÁË¡£

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

½«ÉÏÃæµÄËùÓеĴúÂë¿écopyµ½¿ª·¢¹¤¾ß£¨ÎÒÓõÄÊÇPycharm£©ÖÐÖ±½ÓÆô¶¯¾Í³ÉÁË¡£

Ч¹ûͼÈçÏÂ

»ùÓÚPyQt5ÖÆ×÷Ò»¸ö±íÇé°üÏÂÔØÆ÷

µ½´ËÕâƪ¹ØÓÚ»ùÓÚPyQt5ÖÆ×÷Ò»¸ö±íÇé°üÏÂÔØÆ÷µÄÎÄÕ¾ͽéÉܵ½ÕâÁË,¸ü¶àÏà¹ØPyQt5±íÇé°üÏÂÔØÄÚÈÝÇëËÑË÷Ô´ÂëËѲØÍøÒÔÇ°µÄÎÄÕ»ò¼ÌÐøä¯ÀÀÏÂÃæµÄÏà¹ØÎÄÕÂÏ£Íû´ó¼ÒÒÔºó¶à¶àÖ§³ÖÔ´ÂëËѲØÍø£¡


ÏÂһƪ£ºÃ»ÓÐÁË

ajax½Ì³ÌÔĶÁÅÅÐÐ

×îÐÂÎÄÕÂ