如何使用 Python 拦截广告?

发布于 2024-07-26 07:53:22 字数 703 浏览 3 评论 0原文

我正在 PyQt4 中慢慢构建 网络浏览器 ,并且喜欢我所获得的速度出来了。 但是,我想将 easylist.txt 与它结合起来。 我相信 adblock 使用它来阻止浏览器的 http 请求。

你会如何使用 python/PyQt4 来实现它?

[编辑1] 好的。 我想我已经设置了 Privoxy。 我没有设置任何额外的过滤器,它似乎有效。 我尝试使用的 PyQt4 看起来像这样

self.proxyIP = "127.0.0.1"  
self.proxyPORT= 8118  
proxy = QNetworkProxy()  
proxy.setType(QNetworkProxy.HttpProxy)  
proxy.setHostName(self.proxyIP)  
proxy.setPort(self.proxyPORT)  
QNetworkProxy.setApplicationProxy(proxy)

但是,这绝对没有任何作用,我无法理解文档,也找不到任何示例。

[edit2] 我刚刚注意到,如果我将 self.proxyIP 更改为我的实际本地 IP 而不是 127.0.0.1,则页面不会加载。 所以有些事情正在发生。

I'm slowly building a web browser in PyQt4 and like the speed i'm getting out of it. However, I want to combine easylist.txt with it. I believe adblock uses this to block http requests by the browser.

How would you go about it using python/PyQt4?

[edit1] Ok. I think i've setup Privoxy. I haven't setup any additional filters and it seems to work. The PyQt4 i've tried to use looks like this

self.proxyIP = "127.0.0.1"  
self.proxyPORT= 8118  
proxy = QNetworkProxy()  
proxy.setType(QNetworkProxy.HttpProxy)  
proxy.setHostName(self.proxyIP)  
proxy.setPort(self.proxyPORT)  
QNetworkProxy.setApplicationProxy(proxy)

However, this does absolutely nothing and I cannot make sense of the docs and can not find any examples.

[edit2] I've just noticed that i'f I change self.proxyIP to my actual local IP rather than 127.0.0.1 the page doesn't load. So something is happening.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

嘴硬脾气大 2024-08-02 07:53:22

我知道这是一个老问题,但我想我会尝试为任何偶然发现它的人提供答案。 您可以创建 QNetworkAccessManager 的子类并将其与 https://github.com/atereshkin/abpy 组合。 类似这样的事情:

from PyQt4.QtNetwork import QNetworkAccessManager
from abpy import Filter
adblockFilter = Filter(file("easylist.txt"))
class MyNetworkAccessManager(QNetworkAccessManager):
    def createRequest(self, op, request, device=None):
        url = request.url().toString()
        doFilter = adblockFilter.match(url)
        if doFilter:
            return QNetworkAccessManager.createRequest(self, self.GetOperation, QNetworkRequest(QUrl()))
        else:
            QNetworkAccessManager.createRequest(self, op, request, device)
myNetworkAccessManager = MyNetworkAccessManager()

之后,在所有 QWebView 实例上设置以下内容,或者创建 QWebView 的子类:

QWebView.page().setNetworkAccessManager(myNetworkAccessManager)

希望这会有所帮助!

I know this is an old question, but I thought I'd try giving an answer for anyone who happens to stumble upon it. You could create a subclass of QNetworkAccessManager and combine it with https://github.com/atereshkin/abpy. Something kind of like this:

from PyQt4.QtNetwork import QNetworkAccessManager
from abpy import Filter
adblockFilter = Filter(file("easylist.txt"))
class MyNetworkAccessManager(QNetworkAccessManager):
    def createRequest(self, op, request, device=None):
        url = request.url().toString()
        doFilter = adblockFilter.match(url)
        if doFilter:
            return QNetworkAccessManager.createRequest(self, self.GetOperation, QNetworkRequest(QUrl()))
        else:
            QNetworkAccessManager.createRequest(self, op, request, device)
myNetworkAccessManager = MyNetworkAccessManager()

After that, set the following on all your QWebView instances, or make a subclass of QWebView:

QWebView.page().setNetworkAccessManager(myNetworkAccessManager)

Hope this helps!

℡寂寞咖啡 2024-08-02 07:53:22

这是关于网页过滤的问题吗?

然后尝试使用一些外部网络代理,例如 Privoxy (http://en.wikipedia.org/维基/Privoxy)。

Is this question about web filtering?

Then try use some of external web-proxy, for sample Privoxy (http://en.wikipedia.org/wiki/Privoxy).

卸妝后依然美 2024-08-02 07:53:22

easylist.txt 文件只是纯文本,如下所示:http://adblockplus.mozdev。 org/easylist/easylist.txt

行以 [ 开头,也可以 ! 似乎是注释,所以这只是对文件进行排序,并根据 easylist.txt 文件中行的起始字符在 url/request 中搜索正确的内容。

The easylist.txt file is simply plain text, as demonstrated here: http://adblockplus.mozdev.org/easylist/easylist.txt

lines beginning with [ and also ! appear to be comments, so it is simply a case of sorting through the file, and searching for the correct things in the url/request depending upon the starting character of the line in the easylist.txt file.

ヤ经典坏疍 2024-08-02 07:53:22

Privoxy 很可靠。 如果您希望它完全基于 API,请查看 BrightCloud Web 过滤 API

Privoxy is solid. If you want it to be completely API based though, check out the BrightCloud web filtering API as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文