Skip to content

Commit

Permalink
PyQt5#88 多个用户登录同网站
Browse files Browse the repository at this point in the history
  • Loading branch information
892768447 committed Aug 23, 2019
1 parent 465d7cf commit 4cff257
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ __pycache__/
*.py[co]
*$py.class

Tmp
Tmp/*

# C extensions
*.so

Expand Down
1 change: 1 addition & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 9 additions & 1 deletion QWebEngineView/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [获取Cookie](#1获取Cookie)
- [和Js交互操作](#2和Js交互操作)
- [网页整体截图](#3网页整体截图)
- [同网站不同用户](#4同网站不同用户)

## 1、获取Cookie
[运行 GetCookie.py](GetCookie.py)
Expand All @@ -27,4 +28,11 @@
1. 方式1:目前通过不完美方法(先调整`QWebEngineView`的大小为`QWebEnginePage`的内容大小,等待一定时间后截图再还原大小)
2. 方式2:通过js库`html2canvas`对指定元素截图,得到`base64`编码的数据并调用接口函数传递到py代码中

![ScreenShotPage](ScreenShot/ScreenShotPage.gif)
![ScreenShotPage](ScreenShot/ScreenShotPage.gif)

## 4、同网站不同用户
[运行 SiteDiffUser.py](SiteDiffUser.py)

原理是为每个`QWebEngineView`创建一个`QWebEnginePage`,且使用独立的`QWebEngineProfile`,并配置`persistentStoragePath`不同路径

![SiteDiffUser](ScreenShot/SiteDiffUser.gif)
Binary file added QWebEngineView/ScreenShot/SiteDiffUser.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
60 changes: 60 additions & 0 deletions QWebEngineView/SiteDiffUser.py
Original file line number Diff line number Diff line change
@@ -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_())
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 4cff257

Please sign in to comment.