我们来写一个简单的百度图片下载器,过程如下:
1.百度图片爬虫
2.eric6 和 qt designer 设计 GUI界面
3.将桌面程序和爬虫程序整合在一起
4.打包程序
一、百度图片下载爬虫
这个在爬虫文集里有,实现原理请戳这 爬百度图片
由于咱们是要整合到 GUI 程序里,所以这里将它写为一个类:
import requests
import urllib
import os, re
import itertools
class Spider(object):
def __init__(self, keyword):
self.keyword = keyword
self.urls = self.buildUrls()
self.sign_table = {
'_z2C$q': ':',
'_z&e3B': '.',
'AzdH3F': '/'
}
self.char_table = {
'w': 'a',
'k': 'b',
'v': 'c',
'1': 'd',
'j': 'e',
'u': 'f',
'2': 'g',
'i': 'h',
't': 'i',
'3': 'j',
'h': 'k',
's': 'l',
'4': 'm',
'g': 'n',
'5': 'o',
'r': 'p',
'q': 'q',
'6': 'r',
'f': 's',
'p': 't',
'7': 'u',
'e': 'v',
'o': 'w',
'8': '1',
'd': '2',
'n': '3',
'9': '4',
'c': '5',
'm': '6',
'0': '7',
'b': '8',
'l': '9',
'a': '0'
}
self.char_table = {ord(key): ord(value) for key, value in self.char_table.items()}
def decode(self, url):
for key, value in self.sign_table.items():
url = url.replace(key, value)
return url.translate(self.char_table)
def buildUrls(self):
word = urllib.parse.quote(self.keyword)
url = r"http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&fp=result&queryWord={word}&cl=2&lm=-1&ie=utf-8&oe=utf-8&st=-1&ic=0&word={word}&face=0&istype=2nc=1&pn={pn}&rn=60"
urls = (url.format(word=word, pn=x) for x in itertools.count(start=0, step=60))
return urls
def resolveImgUrl(self, html):
re_url = re.compile(r'"objURL":"(.*?)"')
imgUrls = [self.decode(x) for x in re_url.findall(html)]
return imgUrls
def downImg(self, imgUrl, dirpath, imgName):
filename = os.path.join(dirpath, imgName)
try:
res = requests.get(imgUrl, timeout=15)
if str(res.status_code)[0] == "4":
print(str(res.status_code), ":" , imgUrl)
return False
except Exception as e:
print("抛出异常:", imgUrl)
print(e)
return False
with open(filename, "wb") as f:
f.write(res.content)
return True
def get_path(self):
dirpath = os.getcwd() + '\\pictures'
if not os.path.isdir(dirpath):
os.mkdir(dirpath)
return dirpath
def Download(self):
dirpath = self.get_path()
index = 0
for url in self.urls:
html = requests.get(url, timeout=10).content.decode('utf-8')
imgUrls = self.resolveImgUrl(html)
if len(imgUrls) == 0:
break
for url in imgUrls:
if self.downImg(url, dirpath, str(index) + ".jpg"):
index += 1
print("已下载 %s 张" % index)
return index
if __name__ == "__main__":
a = Spider("美女")
a.Download()
运行该文件将把图片下载该目录下的 picture 文件夹内。
命名为 BaiduSpider.py,先保存下来。
二、接着来设计 GUI 界面
打开 eric6,新建下项目,具体过程见教程(一)里面的链接文章
点击窗体,在空白处右键新建个窗体,就命名为 window吧
可以看到窗口里生成了 window.ui 文件
并且自动跳入 qt designer
先实现最基本的界面,添加俩个 label,文字改为“请输入关键词” 和“————”, 在右上角添加一个lineEdit, 还有俩个 pushbutton 文字改为“开始下载” 和 “退出”。
给退出按钮加上信号槽。
点击 ok ,可以看到退出的动作信号已经好了
保存下文件(工具栏第三个按钮),关闭qt designer 回到 eric6
右键我们刚保存的 window.ui 文件,选择编译窗口,提示编译成功,回到源代码栏,多出了个UI_windows.py文件
右键该文件,打开,在开始中选运行脚本,发现我们刚设计的 GUI 窗口出现了,点击退出,窗口关闭
那么我们一个简单的 GUI 设计到此算完成了
三、整合
关闭 eric6, 复制下UI_windows.py文件,改名为 main.py,以及把第一步的 BaiduSpider.py 也放在这个文件夹
接着编辑 main.py,先打开
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'E:\Python项目\Pyqt\eric\百度图片下载器\window.ui'
#
# Created by: PyQt5 UI code generator 5.6
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(527, 351)
Dialog.setSizeGripEnabled(True)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 70, 101, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(232, 70, 141, 21))
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(60, 170, 161, 31))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(270, 180, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(350, 290, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Dialog)
self.pushButton_2.clicked.connect(Dialog.close)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "百度图片下载器"))
self.label.setText(_translate("Dialog", "请输入关键词:"))
self.label_2.setText(_translate("Dialog", "________________"))
self.pushButton.setText(_translate("Dialog", "开始下载"))
self.pushButton_2.setText(_translate("Dialog", "退出"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
先把开头的注释删掉,然后分析下代码
setupUI 这个方法主要是建立我们的 GUI 布局,retranslateUi 方法则设置了一些文本内容,(由于在设计 GUI 的时候我并没有对每个元素进行相应的命名,刚好可以从这看出元素的名字)
先设置 lineEdit, 在用户输入什么内容时,将内容发送到label_2。
self.lineEdit.textChanged[str].connect(self.onChanged)
在 setupUI方法里关于 lineEdit 添加上面代码,并新增 onChanged 方法。
def onChanged(self, text):
self.label_2.setText(text)
self.label_2.adjustSize()
self.keyword = text
如果 lineEdit的内容发生了改变,即用户输入了内容,就会将内容发送到 label_2 并调整大小。然后创建私有变量 self.keyword(后面百度图片下载要传入的关键词)
在用户输入内容后,点击开始下载则下载图片,所以在 setupUI 里加上
self.pushButton.clicked.connect(self.Download)
添加 Download方法
def Download(self.setText("正在下载")
a = BaiduSpider.Spider(self.keyword)
a.Download()
当点击开始下载后,则将按钮文字改为“正在下载”,创建 BaiduSpider 实例,传入关键词,然后开始下载图片。记得在带面前面 导入BaiduSpider。即
import BaiduSpider
改完代码如下:
from PyQt5 import QtCore, QtGui, QtWidgets
import BaiduSpider
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(527, 351)
Dialog.setSizeGripEnabled(True)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 70, 101, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(232, 70, 141, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.textChanged[str].connect(self.onChanged)
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(60, 170, 161, 31))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(270, 180, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(350, 290, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Dialog)
self.pushButton_2.clicked.connect(Dialog.close)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.Download)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "百度图片下载器"))
self.label.setText(_translate("Dialog", "请输入关键词:"))
self.label_2.setText(_translate("Dialog", "________________"))
self.pushButton.setText(_translate("Dialog", "开始下载"))
self.pushButton_2.setText(_translate("Dialog", "退出"))
def onChanged(self, text):
self.label_2.setText(text)
self.label_2.adjustSize()
self.keyword = text
def Download(self):
self.pushButton.setText("正在下载")
a = BaiduSpider.Spider(self.keyword)
a.Download()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
运行看看
发现有个意外,就是下载时程序就不能被选中了,再点击就会卡死。应该是线程中通信的问题,这个先忽略下。
四、解决阻塞
在上面的程序中,当我们开始下载图片时,由于该下载任务时长较久,就会阻塞程序,详情见这篇文章[http://www.jianshu.com/p/ed47a8959854]
(pyqt多进程防阻塞)
按照这样我们来修改下代码
先导入 QTthread
from PyQt5.QtCore import *
再修改下面这些
from PyQt5 import QtCore, QtGui, QtWidgets
import BaiduSpider
from PyQt5.QtCore import *
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(527, 351)
Dialog.setSizeGripEnabled(True)
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(70, 70, 101, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(Dialog)
self.lineEdit.setGeometry(QtCore.QRect(232, 70, 141, 21))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit.textChanged[str].connect(self.onChanged)
self.label_2 = QtWidgets.QLabel(Dialog)
self.label_2.setGeometry(QtCore.QRect(60, 170, 161, 31))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(Dialog)
self.pushButton.setGeometry(QtCore.QRect(270, 180, 93, 28))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Dialog)
self.pushButton_2.setGeometry(QtCore.QRect(350, 290, 93, 28))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Dialog)
self.pushButton_2.clicked.connect(Dialog.close)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.pushButton.clicked.connect(self.Download)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "百度图片下载器"))
self.label.setText(_translate("Dialog", "请输入关键词:"))
self.label_2.setText(_translate("Dialog", "________________"))
self.pushButton.setText(_translate("Dialog", "开始下载"))
self.pushButton_2.setText(_translate("Dialog", "退出"))
def onChanged(self, text):
self.label_2.setText(text)
self.label_2.adjustSize()
self.keyword = text
def Download(self):
self.pushButton.setText("正在下载")
self.thread = RunThread(self.keyword)
self.thread.start()
class RunThread(QThread):
trigger = pyqtSignal()
def __init__(self,keyword):
super(RunThread, self).__init__()
self.key = keyword
def run(self):
a = BaiduSpider.Spider(self.key)
a.Download()
self.trigger.emit()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
运行下,发现问题已经解决,但图片下载速度好像有点慢
这时我们可以把百度图片爬虫换成多线程的,在上文的文章里有多线程版本,这里就不重复了,同样改成一个类就行。
五、打包程序
用 pyinstaller 就行了,用pip下载完
在该目录下按住 shift 右键打开选择在此打开命令行,输入
pyinstaller -F main.py
过一会儿文件就打包完成了,在 dist 目录下可以发现有个 main.exe文件,挺大的。这个文件可以单独运行了,打开,等一会儿就会跳出我们的 GUI界面,然后就可以下载图片了。
虽然挺简陋的,但起码也是不错的哈
网友评论
trigger = pyqtSignal()
def __init__(self):
super(RunThread, self).__init__()
def set(self,keyword):
self.key=keyword
def run(self):
a = BaiduSpider.Spider(self.key)
a.Download()
self.trigger.emit()