通過 Pyqt5 實作一個界面化的下載器,在通過網路請求實作各種型別的圖片的下載,可以通過界面上輸入不同圖片的關鍵字從而實作下載圖片并將下載好的圖片保存到自定義的檔案路徑中,
文末附原始碼、操作視頻
【閱讀全文】

在介紹代碼塊內容之前,先來看一下需要用到的三方的 python 庫,
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import os
from scripy_images import ScripyImages
其中和 Pyqt5 相關的三個模塊的匯入在前面的天氣查詢系統的介紹中就已經提到過了,若是需要下載《Pyqt5 天氣查詢系統》的原始碼,請在公眾號內回復:"天氣查詢系統",那么剩下的兩個操作庫一個是 os 模塊主要是用來做系統檔案相關操作的,還有一個就是 scripy_images 模塊是用來通過網路請求來獲取和下載圖片的,
接下來介紹 Pyqt5 相關的頁面定義等函式的使用,同樣的先在 Pyqt5 的視窗上面添加我們需要的需要的組件,最后通過將這些組件添加到布局里面,大概的實作程序就是遵循這個順序來實作的,
def init_ui(self):
self.setWindowTitle('百度圖片提取應用')
grid = QGridLayout()
self.page_label = QLabel()
self.page_label.setText('設定爬取頁數:')
self.page_line_text = QLineEdit()
self.page_line_text.setPlaceholderText('輸入整數')
self.page_line_text.setValidator(QIntValidator(1, 99))
self.page_line_text.setFocus()
self.keyword_label = QLabel()
self.keyword_label.setText('設定圖關鍵字:')
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('抓取進度結果展示...')
self.log_text.setReadOnly(True)
self.log_text.setMaximumHeight(100)
self.version_msg_label = QLabel()
self.version_msg_label.setText('<font color=blue>公眾號:[Python 集中營] 發布</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)
再接著就是定義相應的槽函式,其中有兩個槽函式的使用一個是實在定義檔案的存盤的路徑時需要一個槽函式用來將獲取檔案路徑,還有一個就是開始進行百度圖片的下載程序,通過這個槽函式來呼叫下載模塊的執行,
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("-----開始必填項引數檢查-----\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("---引數檢查成功---\n")
check_param = True
else:
self.log_text.insertPlainText("---引數檢查失敗---\n")
self.log_text.insertPlainText("請填寫必填項后繼續...\n")
check_param = False
self.log_text.insertPlainText("-----結束必填項引數檢查-----\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("-----結束下載百度圖片-----\n")
接著就是呼叫主函式執行整個邏輯,
if __name__ == '__main__':
app = QApplication(sys.argv)
baidu = baiduImage()
baidu.show()
sys.exit(app.exec_())
整個 Pyqt5 的呼叫執行程序就是這樣的,獲取完整的代碼塊包括百度圖片執行網路請求下載的部分在公眾號回復"百度圖片下載器"獲取完整代碼塊,
操作視頻演示......

【往期回顧】
python3中的zip()、zip(*)、list()之間的靈活轉換!
python print() 函式的格式化字串輸出
PyQt5 GUI && Requests Api 做一個天氣查詢系統(文末領取完整代碼)!
一款優美的windows cmd命令列工具cmder
如何進行excel資料分析之后的可視化資料寫入保存!
excel資料處理二:快速完成openpyxl資料的新增、修改!
excel資料處理一:巧妙使用openpyxl提取、篩選資料
比Selenium更方便的自動化測驗工具Helium!
Python資料可視化:可視化資料分析插件D-Tale
計算速度太慢?試試 lru_cache 裝飾器!
歡迎關注作者公眾號【Python 集中營】,專注于后端編程,每天更新技術干貨,不定時分享各類資料!轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/386471.html
標籤:其他
