diff --git a/QWebEngineView/BlockRequestData.py b/QWebEngineView/BlockRequestData.py new file mode 100644 index 0000000..00da4fb --- /dev/null +++ b/QWebEngineView/BlockRequestData.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2020年2月18日 +@author: Irony +@site: https://pyqt5.com https://github.com/892768447 +@email: 892768447@qq.com +@file: QWebEngineView.BlockRequestData +@description: 拦截请求内容 +""" +from PyQt5.QtCore import QUrl, QFile, QIODevice +from PyQt5.QtWebEngineCore import QWebEngineUrlSchemeHandler,\ + QWebEngineUrlRequestInterceptor, QWebEngineUrlScheme # @UnresolvedImport +from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile + + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2019' +__Version__ = 'Version 1.0' + + +# 自定义url协议头 +class UrlSchemeHandler(QWebEngineUrlSchemeHandler): + + def requestStarted(self, job): + url = job.requestUrl().toString() + if url == 'myurl://png': + file = QFile('Data/app.png', job) + file.open(QIODevice.ReadOnly) + job.reply(b'image/png', file) + +# 请求拦截器 + + +class RequestInterceptor(QWebEngineUrlRequestInterceptor): + + def interceptRequest(self, info): + url = info.requestUrl().toString() + # 这里演示只是拦截所有png图片,可自由发挥比如拦截js文件,修改后再返回 + if url.endswith('.png'): + # 原理在于重定向到自己的url协议里 + info.redirect(QUrl('myurl://png')) + + +class Window(QWebEngineView): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + self.resize(800, 600) + + # 首先获取默认的url协议 + h1 = QWebEngineUrlScheme.schemeByName(b'http') + h2 = QWebEngineUrlScheme.schemeByName(b'https') + + # 这里需要修改增加本地文件和跨域支持 + CorsEnabled = 0x80 # 5.14才增加 + h1.setFlags(h1.flags() | + QWebEngineUrlScheme.SecureScheme | + QWebEngineUrlScheme.LocalScheme | + QWebEngineUrlScheme.LocalAccessAllowed | + CorsEnabled) + h2.setFlags(h2.flags() | + QWebEngineUrlScheme.SecureScheme | + QWebEngineUrlScheme.LocalScheme | + QWebEngineUrlScheme.LocalAccessAllowed | + CorsEnabled) + + # 安装url拦截器和自定义url协议处理 + de = QWebEngineProfile.defaultProfile() # @UndefinedVariable + de.setRequestInterceptor(RequestInterceptor(self)) + de.installUrlSchemeHandler(b'myurl', UrlSchemeHandler(self)) + + +if __name__ == '__main__': + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = Window() + w.show() + w.load(QUrl('https://www.baidu.com/')) + sys.exit(app.exec_()) diff --git a/QWebEngineView/Data/app.png b/QWebEngineView/Data/app.png new file mode 100644 index 0000000..3f50561 Binary files /dev/null and b/QWebEngineView/Data/app.png differ diff --git a/QWebEngineView/README.md b/QWebEngineView/README.md index bdc894a..533876a 100644 --- a/QWebEngineView/README.md +++ b/QWebEngineView/README.md @@ -6,6 +6,7 @@ - [网页整体截图](#3网页整体截图) - [同网站不同用户](#4同网站不同用户) - [拦截请求](#5拦截请求) + - [拦截请求内容](#6拦截请求内容) ## 1、获取Cookie [运行 GetCookie.py](GetCookie.py) @@ -43,4 +44,12 @@ 通过`QWebEngineUrlRequestInterceptor`中的`interceptRequest`方法对每个请求做拦截过滤 -![BlockRequest](ScreenShot/BlockRequest.gif) \ No newline at end of file +![BlockRequest](ScreenShot/BlockRequest.gif) + +## 6、拦截请求内容 +[运行 BlockRequestData.py](BlockRequestData.py) + +这里用了一个投巧的办法,原理是先通过`QWebEngineUrlRequestInterceptor`中的`interceptRequest`方法对每个请求做拦截过滤, +发现目标url后重定向到`QWebEngineUrlSchemeHandler`实现的自定义协议头返回数据 + +![BlockRequestData](ScreenShot/BlockRequestData.png) \ No newline at end of file diff --git a/QWebEngineView/ScreenShot/BlockRequestData.png b/QWebEngineView/ScreenShot/BlockRequestData.png new file mode 100644 index 0000000..a2999d9 Binary files /dev/null and b/QWebEngineView/ScreenShot/BlockRequestData.png differ diff --git a/README.md b/README.md index 76e8ed1..989bdd2 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ https://pyqt.site 论坛是专门针对PyQt5学习和提升开设的网站,分 - [网页整体截图](QWebEngineView/ScreenShotPage.py) - [同网站不同用户](QWebEngineView/SiteDiffUser.py) - [拦截请求](QWebEngineView/BlockRequest.py) + - [拦截请求内容](QWebEngineView/BlockRequestData.py) - [浏览器下载文件](Test/partner_625781186/6.QWebEngineView下载文件) - [打印网页](Test/partner_625781186/17_打印预览qwebengineview)