浏览器获取Cookie

This commit is contained in:
Irony 2017-12-10 13:45:14 +08:00
parent e55e46b77c
commit c2e68b81da
6 changed files with 146 additions and 1 deletions

View file

@ -17,4 +17,10 @@
### 7.<a href="梦幻树">梦幻树</a><br />
### 8.<a href="自定义属性测试">自定义属性</a><br />
### 8.<a href="自定义属性测试">自定义属性</a><br />
### 9.<a href="自动更新">自动更新</a><br />
### 10.<a href="自定义QWidget的QSS样式">自定义QWidget的QSS样式</a><br />
### 11.<a href="浏览器获取Cookie">浏览器获取Cookie</a><br />

View file

@ -0,0 +1,9 @@
# 获取QWebView或者QWebEngineView的网页Cookie
1.QWebView很简单,从page()中得到QNetworkAccessManager,在从中得到QNetworkCookieJar,
最后得到cookie,当然也可以设置自己的QNetworkCookieJar <br/>
2.QWebEngineView的话目前是通过QWebEngineProfile中得到的cookieStore并绑定它的cookieAdded信号来得到Cookie
# 截图
<img src="ScreenShot/1.png" />
<img src="ScreenShot/2.png" />

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

View file

@ -0,0 +1,83 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年12月10日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: WebEngineView
@description:
'''
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile
from PyQt5.QtWidgets import QApplication
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class WebEngineView(QWebEngineView):
DomainCookies = {} # 存放domain的key-value
PathCookies = {} # 存放domain+path的key-value
def __init__(self, *args, **kwargs):
super(WebEngineView, self).__init__(*args, **kwargs)
# 绑定cookie被添加的信号槽
QWebEngineProfile.defaultProfile().cookieStore(
).cookieAdded.connect(self.onCookieAdd)
self.loadFinished.connect(self.onLoadFinished)
def onLoadFinished(self):
print("*****AllDomainCookies:", self.getAllDomainCookies())
print("*****AllPathCookies:", self.getAllPathCookies())
print("*****alyl.vip cookie:", self.getDomainCookies(".alyl.vip"))
print("*****alyl.vip / path cookie:",
self.getPathCookies(".alyl.vip/"))
def getAllDomainCookies(self):
return self.DomainCookies
def getDomainCookies(self, domain):
return self.DomainCookies.get(domain, {})
def getAllPathCookies(self):
return self.PathCookies
def getPathCookies(self, dpath):
return self.PathCookies.get(dpath, {})
def onCookieAdd(self, cookie):
'''
:param cookie: QNetworkCookie
'''
domain = cookie.domain()
path = cookie.path()
name = cookie.name().data()
value = cookie.value().data()
if domain in self.DomainCookies:
_cookie = self.DomainCookies[domain]
_cookie[name] = value
else:
self.DomainCookies[domain] = {name: value}
domain_path = domain + path
if domain_path in self.PathCookies:
_cookie = self.PathCookies[domain_path]
_cookie[name] = value
else:
self.PathCookies[domain_path] = {name: value}
# print("add cookie:", cookie.domain(),
# cookie.path(), cookie.name(), cookie.value())
if __name__ == "__main__":
app = QApplication(sys.argv)
w = WebEngineView()
w.show()
w.load(QUrl("http://alyl.vip"))
sys.exit(app.exec_())

View file

@ -0,0 +1,47 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Created on 2017年12月10日
@author: Irony."[讽刺]
@site: http://alyl.vip, http://orzorz.vip, https://coding.net/u/892768447, https://github.com/892768447
@email: 892768447@qq.com
@file: WebView
@description:
'''
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWebKitWidgets import QWebView
from PyQt5.QtWidgets import QApplication
__Author__ = "By: Irony.\"[讽刺]\nQQ: 892768447\nEmail: 892768447@qq.com"
__Copyright__ = "Copyright (c) 2017 Irony.\"[讽刺]"
__Version__ = "Version 1.0"
class WebView(QWebView):
def __init__(self, *args, **kwargs):
super(WebView, self).__init__(*args, **kwargs)
self.loadFinished.connect(self.onLoadFinished)
def onLoadFinished(self):
allCookies = self.page().networkAccessManager().cookieJar().allCookies()
print("allCookies:", allCookies)
for cookie in allCookies:
if cookie.domain() == ".alyl.vip":
print("domain:", cookie.domain())
print("path:", cookie.path())
print("name:", cookie.name())
print("value:", cookie.value())
print()
if __name__ == "__main__":
app = QApplication(sys.argv)
w = WebView()
w.show()
w.load(QUrl("http://alyl.vip"))
sys.exit(app.exec_())