QUrl.setUrl 后 PyQt5 QUrl 为空

PyQt5 QUrl is empty after QUrl.setUrl

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

IServ.py

# -*- 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_())

Webview.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 对象(或者说您是立即创建和删除它们)。

此外,创建一个 class 比处理全局变量更好,以避免出现静默错误。

所以我建议你复习你的 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()