python 的 小蜘蛛 我这 哪出问题了 想不明白 哎嘿!

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'widget.ui'
#
# Created by: PyQt5 UI code generator 5.15.10
#
# 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
import lxml.etree as lm
from PyQt5 import QtCore, QtWidgets

# 获取路径
hq = sys.argv[0].split("/")[0:-1]
zh = "/".join(hq)


class Ui_Form(object):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(480, 640)
        self.verticalLayout_2 = QtWidgets.QVBoxLayout(Form)
        self.verticalLayout_2.setObjectName("verticalLayout_2")
        self.verticalLayout = QtWidgets.QVBoxLayout()
        self.verticalLayout.setSpacing(2)
        self.verticalLayout.setObjectName("verticalLayout")
        self.verticalLayout_3 = QtWidgets.QVBoxLayout()
        self.verticalLayout_3.setSpacing(1)
        self.verticalLayout_3.setObjectName("verticalLayout_3")
        self.horizontalLayout = QtWidgets.QHBoxLayout()
        self.horizontalLayout.setSpacing(3)
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.verticalLayout_3.addLayout(self.horizontalLayout)
        self.headertext = QtWidgets.QTextBrowser(Form)
        self.headertext.setMinimumSize(QtCore.QSize(0, 30))
        self.headertext.setMaximumSize(QtCore.QSize(16777215, 30))
        self.headertext.setStyleSheet(
            "#headertext{background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
            "font-family: Serif;\n"
            "text-align: center;\n"
            "border-color: rgb(0, 255, 255);\n"
            "border-style: outset;\n"
            "}")
        self.headertext.setCursorWidth(3)
        self.headertext.setObjectName("headertext")
        self.verticalLayout_3.addWidget(self.headertext)
        self.httpa = QtWidgets.QTextEdit(Form)
        self.httpa.setPlaceholderText("")
        self.httpa.setObjectName("httpa")
        self.verticalLayout_3.addWidget(self.httpa)
        self.showts = QtWidgets.QTextEdit(Form)
        self.showts.setMinimumSize(QtCore.QSize(0, 30))
        self.showts.setMaximumSize(QtCore.QSize(16777215, 30))
        self.showts.setStyleSheet(
            "#showts{background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
            "font-family: Serif;\n"
            "text-align: center;\n"
            "border-color: rgb(0, 255, 255);\n"
            "border-style: outset;\n"
            "}\n"
            "")
        self.showts.setReadOnly(True)
        self.showts.setObjectName("showts")
        self.verticalLayout_3.addWidget(self.showts)
        self.showa = QtWidgets.QTextEdit(Form)
        self.showa.setStyleSheet("#showa{\n"
                                 "color:rgb(0, 255, 127)\n"
                                 "\n"
                                 "\n"
                                 "}")
        self.showa.setReadOnly(True)
        self.showa.setObjectName("showa")
        self.verticalLayout_3.addWidget(self.showa)
        self.lineEdit = QtWidgets.QLineEdit(Form)
        self.lineEdit.setStyleSheet(
            "#lineEdit{background-color: qconicalgradient(cx:0, cy:0, angle:135, stop:0 rgba(255, 255, 0, 69), stop:0.375 rgba(255, 255, 0, 69), stop:0.423533 rgba(251, 255, 0, 145), stop:0.45 rgba(247, 255, 0, 208), stop:0.477581 rgba(255, 244, 71, 130), stop:0.518717 rgba(255, 218, 71, 130), stop:0.55 rgba(255, 255, 0, 255), stop:0.57754 rgba(255, 203, 0, 130), stop:0.625 rgba(255, 255, 0, 69), stop:1 rgba(255, 255, 0, 69));\n"
            "font-family: Serif;\n"
            "text-align: center;\n"
            "border-color: rgb(0, 255, 255);\n"
            "border-style: outset;\n"
            "}")
        self.lineEdit.setReadOnly(True)
        self.lineEdit.setObjectName("lineEdit")
        self.verticalLayout_3.addWidget(self.lineEdit)
        self.xptha = QtWidgets.QTextEdit(Form)
        self.xptha.setObjectName("xptha")
        self.verticalLayout_3.addWidget(self.xptha)
        self.geta = QtWidgets.QPushButton(Form)
        self.geta.setLayoutDirection(QtCore.Qt.LeftToRight)
        self.geta.setAutoFillBackground(False)
        self.geta.setStyleSheet(
            "QPushButton{background-color:qlineargradient(spread:reflect, x1:0.483, y1:0.25, x2:0.995, y2:0.551318, stop:0 rgba(244, 225, 107, 255), stop:1 rgba(255, 255, 255, 255))}\n"
            "\n"
            "QPushButton:pressed{background-color:qconicalgradient(cx:0.5, cy:0.5, angle:0, stop:0 rgba(178, 137, 178, 255), stop:1 rgba(255, 255, 255, 255))}")
        self.geta.setObjectName("geta")
        self.verticalLayout_3.addWidget(self.geta)
        self.posta = QtWidgets.QPushButton(Form)
        self.posta.setStyleSheet(
            "QPushButton{background-color:qlineargradient(spread:reflect, x1:0.483, y1:0.25, x2:0.995, y2:0.551318, stop:0 rgba(244, 225, 107, 255), stop:1 rgba(255, 255, 255, 255))}\n"
            "\n"
            "QPushButton:pressed{background-color:qconicalgradient(cx:0.5, cy:0.5, angle:0, stop:0 rgba(178, 137, 178, 255), stop:1 rgba(255, 255, 255, 255))}")
        self.posta.setObjectName("posta")
        self.verticalLayout_3.addWidget(self.posta)
        self.verticalLayout.addLayout(self.verticalLayout_3)
        self.dataa = QtWidgets.QPushButton(Form)
        self.dataa.setStyleSheet(
            "QPushButton{background-color:qlineargradient(spread:reflect, x1:0.483, y1:0.25, x2:0.995, y2:0.551318, stop:0 rgba(244, 225, 107, 255), stop:1 rgba(255, 255, 255, 255))}\n"
            "\n"
            "QPushButton:pressed{background-color:qconicalgradient(cx:0.5, cy:0.5, angle:0, stop:0 rgba(178, 137, 178, 255), stop:1 rgba(255, 255, 255, 255))}")
        self.dataa.setObjectName("dataa")
        self.verticalLayout.addWidget(self.dataa)
        self.rest = QtWidgets.QPushButton(Form)
        self.rest.setStyleSheet(
            "QPushButton{background-color:qlineargradient(spread:reflect, x1:0.483, y1:0.25, x2:0.995, y2:0.551318, stop:0 rgba(244, 225, 107, 255), stop:1 rgba(255, 255, 255, 255))}\n"
            "\n"
            "QPushButton:pressed{background-color:qconicalgradient(cx:0.5, cy:0.5, angle:0, stop:0 rgba(178, 137, 178, 255), stop:1 rgba(255, 255, 255, 255))}")
        self.rest.setObjectName("rest")
        self.verticalLayout.addWidget(self.rest)
        self.verticalLayout_2.addLayout(self.verticalLayout)

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.headertext.setPlaceholderText(_translate("Form", "以下位置 请填写文章第一页 网址 如:http://www.网站.com/页面 "))
        self.showts.setPlaceholderText(_translate("Form", "校准信息窗口"))
        self.lineEdit.setPlaceholderText(_translate("Form", "xpath设置"))
        self.geta.setText(_translate("Form", "Get方式     "))
        self.posta.setText(_translate("Form", "Post方式    "))
        self.dataa.setText(_translate("Form", "保存文档到..."))
        self.rest.setText(_translate("Form", "       重启 or 重置        "))

        # 按钮事件绑定
        self.geta.clicked.connect(lambda: getmo(self.httpa.toPlainText()))
        self.posta.clicked.connect(lambda: postmo(self.httpa.toPlainText()))
        self.rest.clicked.connect(lambda: resta())
        self.dataa.clicked.connect(lambda: dateb())

    def Detection(self):
        # 判断是否有保存设置
        try:
            with open("{}/Dir.txt".format(zh), "r", encoding="utf-8")as d:
                Dir = d.read()
                self.showts.setText("校对信息窗口~当前保存地址{}".format(Dir))

        except FileNotFoundError:
            self.showts.setText("校对信息窗口~当前无保存地址")


header = {

    "User-Agent": 'Mozilla / 5.0(WindowsNT10.0;Win64;x64) AppleWebKit / 537.36(KHTML, likeGecko) Chrome / 114.0.5735.289Safari / 537.36QIHU360SE'
}


# 获取get 方式内容
def getmo(texta):
    url = f"{texta}" + "get模式"
    ui.showts.setText(url)
    getw = requests.get("{}".format(texta))
    getw.encoding="utf-8"

    if ui.xptha.toPlainText() != "":
        try:
            bc(getw)
            ui.lineEdit.setText("转换中...")
        except:
            ui.lineEdit.setText("你的xpath设置有误!")
    else:
        ui.showa.setText(getw.text)


# 保存函数 有问题..........
def bc(opa):
    ph = ui.xptha.toPlainText() #获取文本
    tq =lm.HTML(opa)
    fx = tq.xpath('{}'.format(ph))
    hu = "\n".join(fx)
    ui.showa.setText("{}".format(hu))


# 获取post 方式内容人
def postmo(textb):
    url = f"{textb}" + "post模式"
    ui.showts.setText(url)
    getp = requests.post("{}".format(textb))
    ui.showa.setText(getp.text)


# 重置窗口
def resta():
    ui.httpa.setText("")
    ui.showa.setText("")
    ui.xptha.setText("")
    ui.showts.setText("重启成功" + "+++++提示窗口")
    ai.update()
    ui.Detection()


# 获取保存地址
def dateb():
    Dir = QtWidgets.QFileDialog.getExistingDirectory()
    if Dir == "":
        ui.showts.setText("您已取消目录选择")



    else:

        with open("{}/Dir.txt".format(zh), "w", encoding="utf-8")as f:
            f.write(Dir)
            datee = "您选择为:" + f"{Dir}" + "~设置成功!"
            ui.showts.setText(datee)


if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    ai = QtWidgets.QWidget()
    ui = Ui_Form()
    ui.setupUi(ai)
    ai.show()
    ui.Detection()
    sys.exit(app.exec_())

我这个想法很简单 : 通过这里设置xpath 来截取 文档 再保存到设置好的文件目录下 

这里就xpath 一有值就会出错  我实在是看不太懂了 ---菜鸟..

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值