diff --git a/.gitignore b/.gitignore index cd34be7..0ca6233 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,9 @@ __pycache__/ *.py[co] *$py.class +Tmp +Tmp/* + # C extensions *.so diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 160c28e..c49089d 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -60,6 +60,7 @@ encoding//QTreeWidget/testTreeWidget.py=utf-8 encoding//QWebEngineView/GetCookie.py=utf-8 encoding//QWebEngineView/JsSignals.py=utf-8 encoding//QWebEngineView/ScreenShotPage.py=utf-8 +encoding//QWebEngineView/SiteDiffUser.py=utf-8 encoding//QWebView/DreamTree.py=utf-8 encoding//QWebView/GetCookie.py=utf-8 encoding//QWebView/JsSignals.py=utf-8 diff --git a/QWebEngineView/README.md b/QWebEngineView/README.md index 803fde1..ff4c083 100644 --- a/QWebEngineView/README.md +++ b/QWebEngineView/README.md @@ -4,6 +4,7 @@ - [获取Cookie](#1获取Cookie) - [和Js交互操作](#2和Js交互操作) - [网页整体截图](#3网页整体截图) + - [同网站不同用户](#4同网站不同用户) ## 1、获取Cookie [运行 GetCookie.py](GetCookie.py) @@ -27,4 +28,11 @@ 1. 方式1:目前通过不完美方法(先调整`QWebEngineView`的大小为`QWebEnginePage`的内容大小,等待一定时间后截图再还原大小) 2. 方式2:通过js库`html2canvas`对指定元素截图,得到`base64`编码的数据并调用接口函数传递到py代码中 -![ScreenShotPage](ScreenShot/ScreenShotPage.gif) \ No newline at end of file +![ScreenShotPage](ScreenShot/ScreenShotPage.gif) + +## 4、同网站不同用户 +[运行 SiteDiffUser.py](SiteDiffUser.py) + +原理是为每个`QWebEngineView`创建一个`QWebEnginePage`,且使用独立的`QWebEngineProfile`,并配置`persistentStoragePath`不同路径 + +![SiteDiffUser](ScreenShot/SiteDiffUser.gif) \ No newline at end of file diff --git a/QWebEngineView/ScreenShot/SiteDiffUser.gif b/QWebEngineView/ScreenShot/SiteDiffUser.gif new file mode 100644 index 0000000..dd135a0 Binary files /dev/null and b/QWebEngineView/ScreenShot/SiteDiffUser.gif differ diff --git a/QWebEngineView/SiteDiffUser.py b/QWebEngineView/SiteDiffUser.py new file mode 100644 index 0000000..89d096a --- /dev/null +++ b/QWebEngineView/SiteDiffUser.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2019年8月23日 +@author: Irony +@site: https://pyqt5.com https://github.com/PyQt5 +@email: 892768447@qq.com +@file: QWebEngineView.SiteDiffUser +@description: 同个网站不同用户 +""" +from PyQt5.QtCore import QUrl +from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEnginePage,\ + QWebEngineProfile +from PyQt5.QtWidgets import QTabWidget + + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2019' +__Version__ = 'Version 1.0' + + +class Window(QTabWidget): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + + # 用户1 + self.webView1 = QWebEngineView(self) + profile1 = QWebEngineProfile('storage1', self.webView1) + profile1.setPersistentStoragePath('Tmp/Storage1') + print(profile1.cookieStore()) + # 如果要清除cookie + # cookieStore = profile1.cookieStore() + # cookieStore.deleteAllCookies() + # cookieStore.deleteSessionCookies() + page1 = QWebEnginePage(profile1, self.webView1) + self.webView1.setPage(page1) + self.addTab(self.webView1, '用户1') + + # 用户2 + self.webView2 = QWebEngineView(self) + profile2 = QWebEngineProfile('storage2', self.webView2) + profile2.setPersistentStoragePath('Tmp/Storage2') + print(profile2.cookieStore()) + page2 = QWebEnginePage(profile2, self.webView2) + self.webView2.setPage(page2) + self.addTab(self.webView2, '用户2') + + self.webView1.load(QUrl('https://v.qq.com')) + self.webView2.load(QUrl('https://v.qq.com')) + + +if __name__ == '__main__': + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = Window() + w.show() + sys.exit(app.exec_()) diff --git a/README.md b/README.md index 55cc5fd..327dc8f 100644 --- a/README.md +++ b/README.md @@ -134,6 +134,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站 - [获取Cookie](QWebEngineView/GetCookie.py) - [和Js交互操作](QWebEngineView/JsSignals.py) - [网页整体截图](QWebEngineView/ScreenShotPage.py) + - [同网站不同用户](QWebEngineView/SiteDiffUser.py) - [浏览器下载文件](Test/partner_625781186/6.QWebEngineView下载文件) - [打印网页](Test/partner_625781186/17_打印预览qwebengineview)