From 0d377225817cbde7956b20fe3468efb102a5fb88 Mon Sep 17 00:00:00 2001 From: Irony <892768447@qq.com> Date: Tue, 24 Sep 2019 16:17:05 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8B=A6=E6=88=AA=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .settings/org.eclipse.core.resources.prefs | 1 + QWebView/BlockRequest.py | 54 +++++++++++++++++++++ QWebView/README.md | 10 +++- QWebView/ScreenShot/BlockRequest.png | Bin 0 -> 4345 bytes README.md | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 QWebView/BlockRequest.py create mode 100644 QWebView/ScreenShot/BlockRequest.png diff --git a/.settings/org.eclipse.core.resources.prefs b/.settings/org.eclipse.core.resources.prefs index 457ff34..75743ce 100644 --- a/.settings/org.eclipse.core.resources.prefs +++ b/.settings/org.eclipse.core.resources.prefs @@ -62,6 +62,7 @@ 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/BlockRequest.py=utf-8 encoding//QWebView/DreamTree.py=utf-8 encoding//QWebView/GetCookie.py=utf-8 encoding//QWebView/JsSignals.py=utf-8 diff --git a/QWebView/BlockRequest.py b/QWebView/BlockRequest.py new file mode 100644 index 0000000..a2edaa1 --- /dev/null +++ b/QWebView/BlockRequest.py @@ -0,0 +1,54 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +""" +Created on 2019年9月24日 +@author: Irony +@site: https://pyqt5.com https://github.com/892768447 +@email: 892768447@qq.com +@file: QWebView.BlockAds +@description: 拦截请求 +""" +from PyQt5.QtCore import QUrl +from PyQt5.QtNetwork import QNetworkAccessManager +from PyQt5.QtWebKitWidgets import QWebView + + +__Author__ = 'Irony' +__Copyright__ = 'Copyright (c) 2019' +__Version__ = 'Version 1.0' + + +class RequestInterceptor(QNetworkAccessManager): + + def createRequest(self, op, originalReq, outgoingData): + """创建请求 + :param op: 操作类型见http://doc.qt.io/qt-5/qnetworkaccessmanager.html#Operation-enum + :param originalReq: 原始请求 + :param outgoingData: 输出数据 + """ + url = originalReq.url().toString() + if url.find('pos.baidu.com') > -1 and url.find('ltu=') > -1: + # 拦截百度联盟的广告 + print('block:', url) + originalReq.setUrl(QUrl()) + + return super(RequestInterceptor, self).createRequest(op, originalReq, outgoingData) + + +class Window(QWebView): + + def __init__(self, *args, **kwargs): + super(Window, self).__init__(*args, **kwargs) + self.resize(800, 600) + self.page().setNetworkAccessManager(RequestInterceptor(self)) + + +if __name__ == '__main__': + import sys + from PyQt5.QtWidgets import QApplication + app = QApplication(sys.argv) + w = Window() + w.show() + w.load(QUrl('https://so.csdn.net/so/search/s.do?q=Qt&t=blog')) + sys.exit(app.exec_()) diff --git a/QWebView/README.md b/QWebView/README.md index 019435b..ad9fc57 100644 --- a/QWebView/README.md +++ b/QWebView/README.md @@ -6,6 +6,7 @@ - [和Js交互操作](#3和Js交互操作) - [网页整体截图](#4网页整体截图) - [播放Flash](#5播放Flash) + - [拦截请求](#6拦截请求) ## 1、梦幻树 [运行 DreamTree.py](DreamTree.py) @@ -46,4 +47,11 @@ 1. 重点在于设置 `os.environ['QTWEBKIT_PLUGIN_PATH'] = os.path.abspath('Data')` ,非常重要,设置为NPSWF32.dll文件所在目录 2. 其次是xp下ssl问题,具体参考代码 -![PlayFlash](ScreenShot/PlayFlash.gif) \ No newline at end of file +![PlayFlash](ScreenShot/PlayFlash.gif) + +## 6、拦截请求 +[运行 BlockRequest.py](BlockRequest.py) + +通过`QNetworkAccessManager`中的`createRequest`方法对每个请求做拦截过滤 + +![BlockRequest](ScreenShot/BlockRequest.gif) \ No newline at end of file diff --git a/QWebView/ScreenShot/BlockRequest.png b/QWebView/ScreenShot/BlockRequest.png new file mode 100644 index 0000000000000000000000000000000000000000..03d4a41b68da56cb19b6fec39ec79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o+8cD4+B9p?Wi2<<#)Fq1wk%J_jh?1@9FIp zFH|g=!a3(srCJe%9q|kPvYLQXwwxR@GyZ=3#Va#y^Kd9`E&u+sbr3bJMq|oHgkF1zM6`@c|I_fB?frZ z9|K464quI6-By4!C@x17`!b48nk_a~sSS*Bx9pdH15MhX5Cy|A;r9&-)w6z#oT_Cc(plmgHYpD$oO< z%r$W3q=-GgI&c`N+V8AKB+0^s$70H%T`~CcGb@=UB*-r$>kMSasowtVWRtk3+_w_e z8}e%Mh*H|Si!i@k9r?k{T7;rfaM;HVI>j6GOXOC>ggw&fhD+Ui1~+iSdI$|CUb~uJ zY;JmTrklYS+B^bOd+goN{*&iBwKgQxE*Id={2mqt7G@I$W$t421D zr~`{P*h7O}Mk#ZLT808t1^(3af0;D?UsV6Yxc*l^Rjd!F)4APm2vQ;0T>&Y K>sRSIJ^ecr?D}{B literal 0 HcmV?d00001 diff --git a/README.md b/README.md index ebb306d..2bb5bcf 100644 --- a/README.md +++ b/README.md @@ -131,6 +131,7 @@ https://pyqt5.com 社区是专门针对PyQt5学习和提升开设的博客网站 - [和Js交互操作](QWebView/JsSignals.py) - [网页整体截图](QWebView/ScreenShotPage.py) - [播放Flash](QWebView/PlayFlash.py) + - [拦截请求](QWebView/BlockRequest.py) - [QWebEngineView](QWebEngineView) - [获取Cookie](QWebEngineView/GetCookie.py) - [和Js交互操作](QWebEngineView/JsSignals.py)