如何将QWebEngineProfile设置为QWebEngineView

时间:2018-01-07 22:58:12

标签: python python-3.x cookies pyqt pyqt5

我想将不同的QWebEngineProfiles设置为不同的QWebEngineViews,这意味着每个视图都有自己的cookie存储。我无法找到任何文件,因此非常感谢所有帮助。 任何关于将独立cookie存储设置为独立webview的方法的建议也会有所帮助。欢呼声。

代码如下(连接信号没有正确格式化,但请确保它在实际代码中是正确的):

from PyQt5.QtCore import *
from PyQt5.QtWidgets import * 
from PyQt5.QtGui import *
from PyQt5.QtWebEngineWidgets import *
import sys

class MainWindow(QMainWindow):

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args,**kwargs)
        self.browser={}
        self.cookiestore={}
        self.page={}
        No = input("No: ")
        for i in range(int(No)):
            self.browser[str(i)] = QWebEngineView()
            storagename = str(i)
            self.cookiestore[str(i)] = QWebEngineProfile(storagename, self.browser[str(i)])
            self.page[str(i)] = QWebEnginePage(self.cookiestore[str(i)], self.browser[str(i)])
            self.browser[str(i)].setPage(self.page[str(i)])
            self.browser[str(i)].load(QUrl("https://www.google.com"))
      self.browser[str(i)].loadFinished.connect(lambda:self._loaded(str(i)))

    def _loaded(self, No):
        self.browser[No].page().toHtml(self._callable)
    def _callable(self, data):
        self.html = data
        if "" in self.html:
            print("Done")
        else:
            print("wait")

app = QApplication(sys.argv)
window = MainWindow()
app.exec_()

1 个答案:

答案 0 :(得分:3)

如果你想建立一个QWebEngineProfile到一个QWebEngineView,你必须通过QWebEnginePage来完成,如下所示:

webview = QWebEngineView()
profile = QWebEngineProfile("somestorage", webview)
webpage = QWebEnginePage(profile, webview)
webview.setPage(webpage)

示例:

from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage
from PyQt5.QtWidgets import QApplication

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    views = []
    for i in range(5):
        webview = QWebEngineView()
        profile = QWebEngineProfile(f"storage-{i}", webview)
        webpage = QWebEnginePage(profile, webview)
        webview.setPage(webpage)
        webview.load(QUrl("https://stackoverflow.com/questions/48142341/how-to-set-a-qwebengineprofile-to-a-qwebengineview"))
        webview.show()
        views.append(webview)
    sys.exit(app.exec_())