获取的文本不是 url,是富文本吗,我可以从边缘浏览器获取 link 吗?

gets text not url, is it rich text, can I get the link from edge browser?

当我从 Edge 浏览器复制 url 并将其粘贴到此处时,我得到 一个 url 例如

https://www.youtube.com/watch?v=-2uyzAqefyE&t=225s

当我将它粘贴到 QTextEdit 中时,我得到看起来像可点击的带下划线的文本 link,但如果我点击它,它不会去任何地方。

<u>PyQt5 Tutorial - Buttons and Events (Signals) - YouTube</u>

如果我使用 QTextEdit.toHtml(),我确实会将 url 作为 href 埋入 标记中(下图)

有没有办法把 url 放进盒子里?

这肯定与improved-copy-and-paste-of-urls-in-microsoft-edge

有关

如果我从 pyperclip 粘贴,我会得到 url。

代码产生:

Qedit url python - QTextEdit gets text not url, is it rich text, can I get the link? - Stack Overflow
Clipboard text 

代码:

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

# Form implementation generated from reading ui file 'spreadsheet_updator.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# 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 pyperclip
from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(898, 216)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.btnGo = QtWidgets.QPushButton(self.centralwidget)
        self.btnGo.setGeometry(QtCore.QRect(70, 100, 91, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.btnGo.setFont(font)
        self.btnGo.setObjectName("btnGo")
        self.txtFile = QtWidgets.QTextEdit(self.centralwidget)
        self.txtFile.setGeometry(QtCore.QRect(70, 50, 811, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.txtFile.setFont(font)
        self.txtFile.setObjectName("txtFile")
        self.lblTitle = QtWidgets.QLabel(self.centralwidget)
        self.lblTitle.setGeometry(QtCore.QRect(20, 10, 211, 21))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(16)
        self.lblTitle.setFont(font)
        self.lblTitle.setObjectName("lblTitle")
        self.prgProgress = QtWidgets.QProgressBar(self.centralwidget)
        self.prgProgress.setGeometry(QtCore.QRect(390, 140, 118, 23))
        self.prgProgress.setProperty("value", 24)
        self.prgProgress.setObjectName("prgProgress")
        self.lblStatus = QtWidgets.QLabel(self.centralwidget)
        self.lblStatus.setGeometry(QtCore.QRect(390, 100, 481, 20))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.lblStatus.setFont(font)
        self.lblStatus.setObjectName("lblStatus")
        self.btnPaste = QtWidgets.QPushButton(self.centralwidget)
        self.btnPaste.setGeometry(QtCore.QRect(10, 50, 51, 31))
        font = QtGui.QFont()
        font.setFamily("Arial")
        font.setPointSize(12)
        self.btnPaste.setFont(font)
        self.btnPaste.setObjectName("btnPaste")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 898, 22))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        self.btnGo.clicked.connect(self.clicked)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.btnGo.setText(_translate("MainWindow", "GO"))
        self.txtFile.setToolTip(_translate("MainWindow", "Paste url or id"))
        self.txtFile.setPlaceholderText(_translate("MainWindow", "URL or ID"))
        self.lblTitle.setText(_translate("MainWindow", "Spreadsheet Updater"))
        self.lblStatus.setText(_translate("MainWindow", "Status:"))
        self.btnPaste.setText(_translate("MainWindow", "Paste"))

    def clicked(self):
        # self.btnGo.clicked.connect(self.clicked)
        url = self.txtFile.toPlainText()
        url2 = pyperclip.paste()
        print('Qedit url ' + url)
        print('Clipboard text ' + url2)



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

从打印输出中可以看出,link 在 html href 中,因此在 plaintText 中看不到。对于这种情况,您可以使用 BeautifulSoup:

from bs4 import BeautifulSoup
html = self.txtFile.toHtml()
soup = BeautifulSoup(html, "html.parser")
el = soup.find(href=True)
if el is not None:
    url = el["href"]
    print(url)

我不知道这个“功能”(我将其标记为 恐怖,更不用说安全问题了它呈现),但考虑到您似乎需要输入字段 type/paste a valid URL 除了 eyllanesc 的解决方案之外,您还有三个选择:

  1. 使用QLineEdit:由于文本字段用于粘贴URLs,因此使用QTextEdit(旨在显示富文本)是没有意义的;考虑到剪贴板的纯文本returns和url,QLineEdit使用剪贴板的纯文本内容,应该从源头上解决问题;
  2. 如果您仍然想要更高级的编辑器(例如,您想要添加多个 urls),您可以设置 acceptRichText 属性 (self.txtFile.setAcceptRichText(False) ) 或在 Designer 中取消选中它,以便它始终使用剪贴板的 纯文本
  3. 对于上述情况,更好的选择是使用 QPlainTextEdit,它默认不接受富文本,但仍支持多行;