gpt4 book ai didi

python - PyQt5 QUrl 在 QUrl.setUrl 之后为空

转载 作者:行者123 更新时间:2023-12-04 09:54:27 24 4
gpt4 key购买 nike

我的python脚本有问题。我想制作一个简单的应用程序,自动登录网站上的人。我认为这可以通过 QUrl 和 QUrlQuery 来完成。这只是 html 中的表单。现在解决我的问题:当我尝试打印 QUrl 时print(login_url.toString(QUrl())) ,它只是打印一个空行。
服务文件

# -*- coding: utf-8 -*-
from PyQt5.QtCore import QUrl, QUrlQuery
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtWidgets

import os
import Webview
import sys


def handle_url_changed():
if webView.url() == QUrl("https://the-server.de/iserv/login"):
login_url = QUrl
login_url.setUrl(QUrl(), "https://the-server.de/iserv/login_check")
# os.system("notify-send " + login_url.toString(QUrl()))

login_query = QUrlQuery
login_query.addQueryItem(QUrlQuery(), "Class", "login-form")
login_query.addQueryItem(QUrlQuery(), "Method", "post")
login_query.addQueryItem(QUrlQuery(), "_username", "...")
login_query.addQueryItem(QUrlQuery(), "_password", "...")
login_query.addQueryItem(QUrlQuery(), "_remember_me", "0")

login_url.setQuery(QUrl(), login_query())
webView.load(login_url())
print(login_url.toString(QUrl()))


app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Webview.Ui_MainWindow()
ui.setupUi(MainWindow)
webView = ui.webView
webView.urlChanged.connect(lambda: handle_url_changed())
MainWindow.show()
sys.exit(app.exec_())

网页 View .py
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Webview.ui'
#
# Created by: PyQt5 UI code generator 5.10.1
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5 import QtWebEngineWidgets


class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(900, 700)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(MainWindow.sizePolicy().hasHeightForWidth())
MainWindow.setSizePolicy(sizePolicy)
MainWindow.setMinimumSize(QtCore.QSize(500, 625))
MainWindow.setFocusPolicy(QtCore.Qt.NoFocus)
MainWindow.setUnifiedTitleAndToolBarOnMac(True)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.webView = QtWebEngineWidgets.QWebEngineView(self.centralwidget)
self.webView.setUrl(QtCore.QUrl("https://the-server.de/"))
self.webView.setObjectName("webView")
self.gridLayout.addWidget(self.webView, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

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

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "IServ"))


if __name__ == "__main__":
import sys

app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())

最佳答案

主要错误是您没有创建 QUrl 和 QUrlQuery 对象(或者您正在立即创建和删除它们)。

此外,最好创建一个类而不是处理全局变量以避免出现静默错误。

所以我建议你查看你的 OOP 笔记。

综上所述,解决方法是:

# -*- coding: utf-8 -*-
import os
import sys

from PyQt5.QtCore import QUrl, QUrlQuery
from PyQt5.QtWidgets import QMessageBox
from PyQt5 import QtWidgets


import Webview


class MainWindow(QtWidgets.QMainWindow, Webview.Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
self.webView.urlChanged.connect(self.handle_url_changed)

def handle_url_changed(self, url):
if url == QUrl("https://the-server.de/iserv/login"):
login_url = QUrl("https://the-server.de/iserv/login_check")
login_query = QUrlQuery()
login_query.addQueryItem("Class", "login-form")
login_query.addQueryItem("Method", "post")
login_query.addQueryItem("_username", "...")
login_query.addQueryItem("_password", "...")
login_query.addQueryItem("_remember_me", "0")
login_url.setQuery(login_query)
self.webview.load(login_url)
print(login_url.toString())


def main():
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())


if __name__ == "__main__":
main()

关于python - PyQt5 QUrl 在 QUrl.setUrl 之后为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61948272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com