[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file '糗事百科查看段子.ui'
#
# Created by: PyQt5 UI code generator 5.15.2
#
# 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
import requests
from PyQt5.QtGui import QImage, QPixmap
from PyQt5.QtWidgets import QLabel, QApplication, QMainWindow, QMessageBox
from PyQt5 import QtCore, QtGui, QtWidgets
from bs4 import BeautifulSoup
class Ui_Form(object):
def __init__(self):
self.duanzi = []
self.url = 'https://www.qiushibaike.com/imgrank/page/1/'
self.headers = {
"Host": "www.qiushibaike.com",
"If-None-Match": "\"035e1441b9c1db3fd182c7b6b8e1e97eed8032b9\"",
"Referer": "https://www.qiushibaike.com/text/",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.81 Safari/537.36 SE 2.X MetaSr 1.0"
}
self.page = 1
self.number = 0
self.lenth = 0
self.imglist = []
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(734, 624)
self.qLabel = QtWidgets.QLabel(Form)
self.qLabel.setGeometry(QtCore.QRect(190, 10, 361, 271))
self.qLabel.setObjectName("qLabel")
self.qLabel.setScaledContents(True)
self.label_2 = QtWidgets.QLabel(Form)
self.label_2.setGeometry(QtCore.QRect(150, 330, 451, 161))
self.label_2.setObjectName("label_2")
self.pushButton_3 = QtWidgets.QPushButton(Form)
self.pushButton_3.setGeometry(QtCore.QRect(270, 540, 211, 61))
self.pushButton_3.setObjectName("pushButton_3")
self.pushButton_4 = QtWidgets.QPushButton(Form)
self.pushButton_4.setGeometry(QtCore.QRect(570, 540, 101, 61))
self.pushButton_4.setObjectName("pushButton_4")
self.pushButton_5 = QtWidgets.QPushButton(Form)
self.pushButton_5.setGeometry(QtCore.QRect(90, 540, 101, 61))
self.pushButton_5.setObjectName("pushButton_5")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.pushButton_3.clicked.connect(self.showduanzi)
self.pushButton_4.clicked.connect(self.next)
self.pushButton_5.clicked.connect(self.onA)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "糗事百科查看段子"))
self.label_2.setText(_translate("Form", "文字"))
self.pushButton_3.setText(_translate("Form", "看段子"))
self.pushButton_4.setText(_translate("Form", "下一条"))
self.pushButton_5.setText(_translate("Form", "上一条"))
def showduanzi(self):
req = requests.get(self.url, headers=self.headers)
content = BeautifulSoup(req.content.decode(), 'lxml').select('div.content>span')
img = BeautifulSoup(req.content.decode(), 'lxml').select('div.thumb>a>img')
self.imglist = [x.get('src') for x in img]
self.lenth = len(content)
self.duanzi = [x.text.strip() for x in content]
self.label_2.setText(self.duanzi[0])
res = requests.get('https:' + self.imglist[0])
img = QImage.fromData(res.content)
self.qLabel.setPixmap(QPixmap.fromImage(img))
print(self.duanzi)
def next(self):
self.number += 1
print(self.number)
if self.number == self.lenth - 1:
self.page += 1
QMessageBox.information(w, '提示', '没有更多了哦')
QMessageBox.information(w, '提示', '点击确定后开始下一页')
self.url = f'https://www.qiushibaike.com/imgrank/page/{self.page}/'
print(self.url)
self.number = 0 # 初始化
self.lenth = 0
self.imglist = []
self.showduanzi()
else:
ur = self.imglist[self.number]
res = requests.get('https:' + ur)
img = QImage.fromData(res.content)
self.qLabel.setPixmap(QPixmap.fromImage(img))
self.label_2.setText(self.duanzi[self.number])
def onA(self):
self.number -= 1
print(self.number)
if self.number < 0:
self.page += 1
QMessageBox.information(w, '提示', '这已经是最后一张啦!')
self.showduanzi()
else:
ur = self.imglist[self.number - 1]
res = requests.get('https:' + ur)
img = QImage.fromData(res.content)
self.qLabel.setPixmap(QPixmap.fromImage(img))
self.label_2.setText(self.duanzi[self.number])
if __name__ == '__main__':
app = QApplication(sys.argv)
w = QMainWindow()
UI = Ui_Form()
UI.setupUi(w)
w.show()
app.exec_()