[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'expressionWeb.ui'
#
# Created by: PyQt5 UI code generator 5.15.1
#
# 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.
import sys
from datetime import datetime
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QThread, pyqtSignal
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QMainWindow, QFileDialog, QMessageBox
import os
import requests
from bs4 import BeautifulSoup
import re
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(371, 329)
self.progressBar = QtWidgets.QProgressBar(Form)
self.progressBar.setGeometry(QtCore.QRect(117, 91, 151, 23))
self.progressBar.setProperty("value", 0)
self.progressBar.setObjectName("progressBar")
self.accordingToSituation = QtWidgets.QTextBrowser(Form)
self.accordingToSituation.setGeometry(QtCore.QRect(0, 182, 371, 148))
self.accordingToSituation.setObjectName("accordingToSituation")
# self.accordingToSituation.setLineWrapMode(1)
self.progressOf = QtWidgets.QLabel(Form)
self.progressOf.setGeometry(QtCore.QRect(70, 91, 72, 23))
self.progressOf.setObjectName("progressOf")
self.layoutWidget = QtWidgets.QWidget(Form)
self.layoutWidget.setGeometry(QtCore.QRect(33, 49, 311, 25))
self.layoutWidget.setObjectName("layoutWidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.layoutWidget)
self.horizontalLayout.setContentsMargins(0, 0, 0, 0)
self.horizontalLayout.setObjectName("horizontalLayout")
self.label = QtWidgets.QLabel(self.layoutWidget)
self.label.setObjectName("label")
self.horizontalLayout.addWidget(self.label)
self.numberPages = QtWidgets.QLineEdit(self.layoutWidget)
self.numberPages.setObjectName("numberPages")
self.horizontalLayout.addWidget(self.numberPages)
self.grab = QtWidgets.QPushButton(self.layoutWidget)
self.grab.setObjectName("grab")
self.horizontalLayout.addWidget(self.grab)
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(110, 140, 121, 23))
self.pushButton.setObjectName("pushButton")
# self.pushButton.clicked.connect(self.slot_btn_chooseDir)
self.grab.clicked.connect(self.just)
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "表情包软件"))
self.progressOf.setText(_translate("Form", "进度"))
self.label.setText(_translate("Form", " 输入页数:"))
self.grab.setText(_translate("Form", "抓取"))
self.pushButton.setText(_translate("Form", "功能待定"))
def settxt(self,wd):
self.accordingToSituation.setText(wd)
def GrabExpression(self, data):
self.progressBar.setValue(data)
# self.updatas()
# self.accordingToSituation.setText(data)
if data == 100:
# QMessageBox.information(w, '提示', '保存成功')
# self.progressBar.setValue(0)
# self.accordingToSituation.setText('保存成功')
# self.grab.setText('保存成功')
QMessageBox.information(w,'提示','保存成功')
self.numberPages.setText('')
self.progressBar.setValue(0)
self.grab.setText('抓取')
else:
pass
def just(self):
# global bs
# bs=self.slot_btn_chooseDir()
self.update_data_thread = UpdateData()
self.update_data_thread.update_date.connect(self.GrabExpression)
self.update_data_thread.upda_name.connect(self.stext)
self.update_data_thread.start()
def toObtainInputBox(self):
urls=self.numberPages.text()
return urls
def erross(self):
self.grab.setText('失败')
# def updatas(self):
# self.update_data_thread = UpdateData()
# self.update_data_thread.upda_name.connect(self.GrabExpression)
# self.update_data_thread.start()
def stext(self,datas):
self.accordingToSituation.insertPlainText(datas)
class UpdateData(QThread,Ui_Form):
"""更新数据类"""
update_date = pyqtSignal(int)
upda_name=pyqtSignal(str) # pyqt5 支持python3的str,没有Qstring
def run(self):
t=1
a = 0
page=int(UI.toObtainInputBox())
curr_time = datetime.datetime.now()#获取系统时间
fw=100/page
path = datetime.datetime.strftime(curr_time, '%Y%m%d%H%M')
# print(path)
if os.path.exists(path):#检查是否存在这个文件夹
# print("属于这个时间点的文件夹已经创建好")
pass
else:
os.mkdir(path)#不存在则创建
print("创建成功!!!!正在保存图片")
dirname = os.getcwd() + '\\' + path + '\\'
print(dirname)
while t<=page:
# ase=UI.slot_btn_chooseDir()
try:# test=self.xuanquwenjian()
url=f'https://fabiaoqing.com/biaoqing/lists/page/{t}.html'#获取连接
# print(curr_time)#打印时间 测试用
headers = {
'user-agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1'}# 'cookie': 'tvfe_boss_uuid=4427f26b6d83d5d7; pgv_pvid=8192465356; pgv_pvi=2750494720; RK=cfw14pvSFY; ptcz=026939cd8bdd917551be81f3d0d2563bdb9e2d0805f4c83de8df0ea6af457e49; eas_sid=i1e690x1l8v2I68559J4e8K995; LW_sid=W1C6S0u1y8a2A6E864o8L480Z0; LW_uid=51H6V041L8i2n6Q8M4S8e4k0D0; uin_cookie=o0878530130; ied_qq=o0878530130; o_cookie=878530130; pac_uid=1_878530130; luin=o0878530130; lskey=000100000f95a236a0b3f6a309a1f6e4809612024104f9a476a9b0803995ce53ec225971d5d95f3164c7df7a; rewardsn=; wxtokenkey=777'}
req = requests.get(url=url, headers=headers).content.decode()
except Exception as e:
# QMessageBox.information(w,'提示','报错!!!')
UI.erross()
return
soup = BeautifulSoup(req, 'lxml')#用BeautifulSoup解析网页
res = soup.select('img[class="ui image lazy"]')
# cd=len(res)
# fw=100/cd#获取该网页中所有的图片标签
for i in res:#遍历所有的图片标签
if i.get("data-original") == None:#如果这个标签内的data-original等于空的时候直接跳过
pass
else:
try:#尝试去保存图片 如果保存图片错误则抛出异常
# UI.stext(ase)
with open(dirname + f'{i.get("alt")}'+os.path.splitext(i.get("data-original"))[-1], 'wb') as f:#拼接路径+a.jpg a是等于数字 每添加一个 a自增一 相当于是给图片命名 并且以二进制的形式写入
f.write(requests.get(url=i.get("data-original"), headers=headers).content)#向这个图片发送请求 并将图片的二进制写入
f.close()
#关闭写入
self.upda_name.emit(f'已保存第{a}张图片\n') # 发射信号
a = a + 1#a自增一
except Exception as e:#抛出异常 增加程序强壮性
print("出现异常了")
# self.upda_name.emit(f'=============================================================\n') # 发射信号
print("保存成功")
t=t+1 # 发射信号
self.update_date.emit((fw*t)-1)
self.update_date.emit(100)
if __name__ == "__main__":
app = QApplication(sys.argv)
w = QMainWindow()
UI = Ui_Form()
UI.setupUi(w)
w.setWindowIcon(QIcon("ts.ico"))
w.show()
app.exec_()