当爬虫遇到PyQt5:GUI界面实现百度图片爬取
Posted 小泽yyds
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了当爬虫遇到PyQt5:GUI界面实现百度图片爬取相关的知识,希望对你有一定的参考价值。
目录
1 成品图
2 核心代码展示
爬取操作
url = 'https://image.baidu.com/search/flip?tn=baiduimage&ie=utf-8&word=' + reptileWord + '&pn='
num = 1
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/80.0.3987.122 Safari/537.36'
}
response = requests.Session()
response.headers = headers
html = response.get(url, timeout=10, allow_redirects=False)
pic_urls = re.findall('"objURL":"(.*?)",', html.text, re.S)
for pic_url in pic_urls:
download_value = '正在下载第' + str(num) + '张图片,图片url:' + str(pic_url)
string = savePath + r'\\\\' + reptileWord + '+' + str(num) + '.jpg'
local_path = open(string, 'wb')
photo = requests.get(pic_url, timeout=7)
local_path.write(photo.content)
local_path.close()
num += 1
实现选择文件夹并返回文本框路径操作
def select_folder(self):
self.directory = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹", "C:/") # 起始路径
directory = str(self.directory)
self.le1.setText(directory)
若无任何填入点击“start”按钮出现弹窗
if self.le0.text()=='' or self.le1.text()=='':
QMessageBox.information(self, "提示", "请输入有效字符!")
实现进度条
def timerEvent(self, e):
if self.step >= 100:
self.step = 0
self.pbar.setValue(self.step)
self.timer.stop()
return
self.step = self.step+1
self.pbar.setValue(self.step)
def doAction(self, value):
if self.timer.isActive():
self.timer.stop()
else:
self.timer.start(100, self)
3 如何获取完整代码
请移步我的GitHub页面:https://github.com/zhao302014/PyQt5/tree/main/reptile
以上是关于当爬虫遇到PyQt5:GUI界面实现百度图片爬取的主要内容,如果未能解决你的问题,请参考以下文章