吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2085|回复: 13
收起左侧

[Python 转载] 用python写的糗事百科低配版客户端 我只写了带图片的的段子其他还没做 发出来给大...

[复制链接]
cdsgg 发表于 2021-3-26 16:03
[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_()
1.gif
1.png

免费评分

参与人数 2吾爱币 +2 热心值 +1 收起 理由
ingdear + 1 害,本来在好好的写PY搬砖,结果你整这么个,又看了几小时糗百,-_-!!
PhoebeCLS + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| cdsgg 发表于 2021-3-26 16:09
枕头ER 发表于 2021-3-26 16:08
感谢感谢,真的学到了很多,之前我都不知道如何操作

好好看 好好学
枕头ER 发表于 2021-3-26 16:08
感谢感谢,真的学到了很多,之前我都不知道如何操作
王雪峰 发表于 2021-3-26 16:15
Wapj_Wolf 发表于 2021-3-26 16:16
PY小白学习了,谢谢分享……
PhoebeCLS 发表于 2021-3-26 16:32
哈哈不错学习学习,厉害啊
雨陌 发表于 2021-3-26 16:48
还行吧,简单的爬虫案例。
52changew 发表于 2021-3-26 16:49
路过; 看看; 学习了解下; 谢谢!
qianshang666 发表于 2021-3-26 16:50
学习了,谢谢分享
 楼主| cdsgg 发表于 2021-3-26 16:50
雨陌 发表于 2021-3-26 16:48
还行吧,简单的爬虫案例。

可能你理解错了 主要看的是pyqt
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 18:57

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表