PyQt5 實(shí)現(xiàn)百度圖片下載器GUI界面
通過 Pyqt5 實(shí)現(xiàn)一個(gè)界面化的下載器,在通過網(wǎng)絡(luò)請求實(shí)現(xiàn)各種類型的圖片的下載??梢酝ㄟ^界面上輸入不同圖片的關(guān)鍵字從而實(shí)現(xiàn)下載圖片并將下載好的圖片保存到自定義的文件路徑中。

在介紹代碼塊內(nèi)容之前,先來看一下需要用到的三方的 python 庫。
from PyQt5.QtWidgets import * from PyQt5.QtCore import * from PyQt5.QtGui import * import os from scripy_images import ScripyImages
那么剩下的兩個(gè)操作庫一個(gè)是 os 模塊主要是用來做系統(tǒng)文件相關(guān)操作的,還有一個(gè)就是 scripy_images 模塊是用來通過網(wǎng)絡(luò)請求來獲取和下載圖片的。
接下來介紹 Pyqt5 相關(guān)的頁面定義等函數(shù)的使用。同樣的先在 Pyqt5 的窗口上面添加我們需要的需要的組件,最后通過將這些組件添加到布局里面,大概的實(shí)現(xiàn)過程就是遵循這個(gè)順序來實(shí)現(xiàn)的。
def init_ui(self):
self.setWindowTitle('百度圖片提取應(yīng)用')
grid = QGridLayout()
self.page_label = QLabel()
self.page_label.setText('設(shè)置爬取頁數(shù):')
self.page_line_text = QLineEdit()
self.page_line_text.setPlaceholderText('輸入整數(shù)')
self.page_line_text.setValidator(QIntValidator(1, 99))
self.page_line_text.setFocus()
self.keyword_label = QLabel()
self.keyword_label.setText('設(shè)置圖關(guān)鍵字:')
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('輸入漢字')
self.file_path = QLineEdit()
self.file_path.setPlaceholderText('自定義文件路徑')
self.file_path.setReadOnly(True)
self.file_path_button = QPushButton()
self.file_path_button.setText('自定義路徑')
self.file_path_button.clicked.connect(self.file_path_click)
self.request_button = QPushButton()
self.request_button.setText('快速開始抓取圖片')
self.request_button.clicked.connect(self.download_image)
self.log_text = QTextEdit()
self.log_text.setPlaceholderText('抓取進(jìn)度結(jié)果展示...')
self.log_text.setReadOnly(True)
self.log_text.setMaximumHeight(100)
self.version_msg_label = QLabel()
self.version_msg_label.setText('<font color=blue>公眾號:[Python 集中營] 發(fā)布</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)
再接著就是定義相應(yīng)的槽函數(shù),其中有兩個(gè)槽函數(shù)的使用一個(gè)是實(shí)在定義文件的存儲(chǔ)的路徑時(shí)需要一個(gè)槽函數(shù)用來將獲取文件路徑。還有一個(gè)就是開始進(jìn)行百度圖片的下載過程,通過這個(gè)槽函數(shù)來調(diào)用下載模塊的執(zhí)行。
def file_path_click(self):
self.cwd = os.getcwd()
directory = QFileDialog.getExistingDirectory(self, '選取文件夾', self.cwd)
print(directory)
self.file_path.setText(directory + '/')
def download_image(self):
check_param = False
self.log_text.setText("")
self.log_text.insertPlainText("-----開始必填項(xiàng)參數(shù)檢查-----\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("---參數(shù)檢查成功---\n")
check_param = True
else:
self.log_text.insertPlainText("---參數(shù)檢查失敗---\n")
self.log_text.insertPlainText("請?zhí)顚懕靥铐?xiàng)后繼續(xù)...\n")
check_param = False
self.log_text.insertPlainText("-----結(jié)束必填項(xiàng)參數(shù)檢查-----\n")
if check_param is True:
self.log_text.insertPlainText("-----開始下載百度圖片-----\n")
self.log_text.insertPlainText("---請耐心等待---\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("-----結(jié)束下載百度圖片-----\n")
接著就是調(diào)用主函數(shù)執(zhí)行整個(gè)邏輯。
if __name__ == '__main__':
app = QApplication(sys.argv)
baidu = baiduImage()
baidu.show()
sys.exit(app.exec_())
整個(gè) Pyqt5 的調(diào)用執(zhí)行過程就是這樣的?
到此這篇關(guān)于PyQt5 實(shí)現(xiàn)百度圖片下載器GUI界面的文章就介紹到這了,更多相關(guān)PyQt5百度圖片下載器GUI界面內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python Pytorch深度學(xué)習(xí)之核心小結(jié)
今天小編就為大家分享一篇關(guān)于Pytorch核心小結(jié)的文章,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-10-10
django實(shí)現(xiàn)后臺(tái)顯示媒體文件
這篇文章主要介紹了django實(shí)現(xiàn)后臺(tái)顯示媒體文件,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04
pytho多張圖片的無損拼接的實(shí)現(xiàn)示例
很多人都會(huì)是用PS進(jìn)行拼接,本文主要介紹了pytho多張圖片的無損拼接的實(shí)現(xiàn)示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
使用pyqt 實(shí)現(xiàn)重復(fù)打開多個(gè)相同界面
今天小編就為大家分享一篇使用pyqt 實(shí)現(xiàn)重復(fù)打開多個(gè)相同界面,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
解決selenium模塊利用performance獲取network日志請求報(bào)錯(cuò)的問題(親測有效)
這篇文章主要介紹了解決selenium模塊利用performance獲取network日志請求報(bào)錯(cuò)的問題(親測有效),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-03-03
python使用socket進(jìn)行簡單網(wǎng)絡(luò)連接的方法
這篇文章主要介紹了python使用socket進(jìn)行簡單網(wǎng)絡(luò)連接的方法,實(shí)例分析了Python使用socket的基本技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
Python數(shù)據(jù)可視化之使用matplotlib繪制簡單圖表
這篇文章主要為大家詳細(xì)介紹了使用matplotlib繪制簡單圖表的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03

