Python爬虫,使用Python制作网络图片下载器软件,一键下载需要的网络图片

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、图片下载软件是什么?
  • 二、具体制作步骤
    • 1.使用Qt Creator制作软件界面
    • 2.主程序
  • 最后
  • 总结


前言

提示:这里可以添加本文要记录的大概内容:


提示:以下是本篇文章正文内容,下面案例可供参考

一、图片下载软件是什么?

图片下载软件能够根据所要下载的图片名称,从网络上进行爬取相关名称或者内容的图片进行下载并保存。

二、具体制作步骤

1.使用Qt Creator制作软件界面

代码如下(示例):
首先使用Qt Creator制作软件界面,保存得到.ui文件,接着在命令行中使用下面的命令将MainWindow.ui文件转为ui_MainWindow.py文件,文件名可以自己设定。

pyuic5 -o ui_MainWindow.py MainWindow.ui

下面是我自己设定的ui界面,供大家参考。

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.6
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(479, 255)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.horizontalLayout_5 = QtWidgets.QHBoxLayout(self.centralwidget)
        self.horizontalLayout_5.setObjectName("horizontalLayout_5")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setMaximumSize(QtCore.QSize(180, 49))
        self.label.setObjectName("label")
        self.horizontalLayout.addWidget(self.label)
        self.textEdit = QtWidgets.QTextEdit(self.centralwidget)
        self.textEdit.setMaximumSize(QtCore.QSize(213, 49))
        self.textEdit.setObjectName("textEdit")
        self.horizontalLayout.addWidget(self.textEdit)
        self.verticalLayout.addLayout(self.horizontalLayout)
        self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setMaximumSize(QtCore.QSize(105, 49))
        self.label_2.setObjectName("label_2")
        self.horizontalLayout_2.addWidget(self.label_2)
        self.progressBar = QtWidgets.QProgressBar(self.centralwidget)
        self.progressBar.setMaximumSize(QtCore.QSize(228, 23))
        self.progressBar.setProperty("value", 24)
        self.progressBar.setObjectName("progressBar")
        self.horizontalLayout_2.addWidget(self.progressBar)
        self.verticalLayout.addLayout(self.horizontalLayout_2)
        self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_3.setObjectName("horizontalLayout_3")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setMaximumSize(QtCore.QSize(111, 49))
        self.label_3.setObjectName("label_3")
        self.horizontalLayout_3.addWidget(self.label_3)
        self.spinBox = QtWidgets.QSpinBox(self.centralwidget)
        self.spinBox.setMaximumSize(QtCore.QSize(101, 31))
        self.spinBox.setObjectName("spinBox")
        self.horizontalLayout_3.addWidget(self.spinBox)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
        self.horizontalLayout_4.setObjectName("horizontalLayout_4")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setMaximumSize(QtCore.QSize(91, 41))
        self.pushButton.setObjectName("pushButton")
        self.horizontalLayout_4.addWidget(self.pushButton)
        self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_2.setMaximumSize(QtCore.QSize(91, 41))
        self.pushButton_2.setObjectName("pushButton_2")
        self.horizontalLayout_4.addWidget(self.pushButton_2)
        self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_3.setMaximumSize(QtCore.QSize(91, 41))
        self.pushButton_3.setObjectName("pushButton_3")
        self.horizontalLayout_4.addWidget(self.pushButton_3)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout_5.addLayout(self.verticalLayout)
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 479, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton_2.clicked.connect(self.spinBox.clear) # type: ignore
        self.pushButton_2.clicked.connect(self.textEdit.clear) # type: ignore
        self.pushButton_3.clicked.connect(MainWindow.close) # type: ignore
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.label.setText(_translate("MainWindow", "请输入需要下载的图片名称"))
        self.label_2.setText(_translate("MainWindow", "图片下载的进度"))
        self.label_3.setText(_translate("MainWindow", "下载的图片页数"))
        self.pushButton.setText(_translate("MainWindow", "下载"))
        self.pushButton_2.setText(_translate("MainWindow", "清空"))
        self.pushButton_3.setText(_translate("MainWindow", "退出"))

2.主程序

代码如下(示例):

import sys
import requests
import os
import re
from ui_MainWindow import Ui_MainWindow
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QMainWindow, QMessageBox, QApplication


class QmyMainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.ui.progressBar.setValue(0)

    @pyqtSlot(int)
    def on_spinBox_valueChanged(self, arg1):
        self.num=self.ui.spinBox.value()

    @pyqtSlot(bool)
    def on_pushButton_clicked(self,checked):
        if checked==False:
           self.name=self.ui.textEdit.toPlainText() #得到输入的图片名称
           if self.name=='':
               d='Warning'
               s='请输入要下载的图片名称'
               QMessageBox.warning(self,d,s)
           elif self.num<=0:
               d = 'Warning'
               s = '请输入需要下载的图片数量'
               QMessageBox.warning(self, d, s)
           else:
               self.save_path='C:\\Users\\29536\\Desktop\\图片\\'+self.name
               #下载图片
               self.get_image(self.num)
    def get_image(self,page_num):

        global param
        header={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
                'X-Requested-With':'XMLHttpRequest'
                }
        url='https://image.baidu.com/search/acjson?'
        self.image_url=[]
        for pn in range(0,30*page_num,30):
            param={
                'tn': 'resultjson_com',
                'ipn': 'rj',
                'ct': 201326592,
                'is': 0,
                'fp': 'detail',
                'cl': 2,
                'lm': -1,
                'ie': 'utf-8',
                'oe': 'utf-8',
                'adpicid': 0,
                'lpn': 0,
                'st':'',
                'word':self.name,
                'z': 0,
                'ic':'',
                'hd':'',
                'latest':'',
                'copyright':'',
                's':'',
                'se':'',
                'tab':'',
                'width':'',
                'height':'',
                'face':'',
                'istype':'',
                'qc':'',
                'nc':'',
                'fr': 'ala',
                'simics':'',
                'srctype':'',
                'bdtype': 0,
                'rpstart':'',
                'rpnum':'',
                'cs': 2382573153,
                'catename':'',
                'nojc':'',
                'album_id':'',
                'album_tab':'',
                'cardserver':'',
                'tabname':'',
                'pn': pn,
                'rn': '30',
                'gsm': '1',
                '1651558323952':''
            }
            request=requests.get(url=url,headers=header,params=param)
            # if request.status_code==200:
            #     print('requests success')
            request.encoding='utf-8'
            html=request.text
            # print(html)
            image_url_list=re.findall('"thumbURL":"(.*?)","',html,re.S)
            # print(len(image_url_list))
            for i in image_url_list:
                self.image_url.append(i)
                # print(i)
        if not os.path.exists(self.save_path):
             os.makedirs(self.save_path)
        self.ui.progressBar.setMaximum(len(self.image_url))
        print(len(self.image_url))
        n=0
        for i in self.image_url:
            image_data=requests.get(url=i,headers=header,params=param).content
            with open(os.path.join(self.save_path,f'{n:06d}.jpg'),'wb') as fp:
                fp.write(image_data)
            n=n+1
            self.ui.progressBar.setValue(n)
if __name__ == '__main__':
    app=QApplication(sys.argv)
    form=QmyMainWindow()
    form.show()
    sys.exit(app.exec_())


最后

使用了pyinstaller对于该程序进行了软件打包,由于对于使用Pyqt5制作的软件,在打包后,所生成的程序在别人的电脑中无法使用,因此,在打包的过程中,需要添加python运行程序的环境中的Pyqt5模块的bin文件夹的路径。需要在python程序的运行环境中,并打开以上两个.py文件所在的文件夹,接着使用打包命令进行打包,使用的打包命令如下:

pyinstaller --path D:\Anaconda3\envs\python39\Lib\site-packages\PyQt5\Qt5\bin -F -w main.py -p ui_MainWindow.py

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了网络图片下载软件的大致制作步骤,以及代码,而具体的视频教程,我发布在了西瓜视频上,大家有需要的可以点击观看,视频地址

https://www.ixigua.com/7093542163917013518?utm_source=xiguastudio

代码下载地址

链接: https://wwm.lanzoul.com/isbLH0537wcf

你可能感兴趣的:(python爬虫,python)