简体   繁体   English

你会如何使用Python进行广告拦截?

[英]How would you adblock using Python?

I'm slowly building a web browser in PyQt4 and like the speed i'm getting out of it. 我正在慢慢地在PyQt4中构建一个Web浏览器 ,就像我正在摆脱它的速度一样。 However, I want to combine easylist.txt with it. 但是,我想将easylist.txt与它结合起来。 I believe adblock uses this to block http requests by the browser. 我相信adblock使用它来阻止浏览器的http请求。

How would you go about it using python/PyQt4? 你会如何使用python / PyQt4进行操作?

[edit1] Ok. [edit1]好的。 I think i've setup Privoxy. 我想我已经设置了Privoxy。 I haven't setup any additional filters and it seems to work. 我没有设置任何额外的过滤器,它似乎工作。 The PyQt4 i've tried to use looks like this 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)

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. [edit2]我刚刚注意到,如果我将self.proxyIP更改为我的实际本地IP而不是127.0.0.1,则页面无法加载。 So something is happening. 所以有些事情正在发生。

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 . 您可以创建QNetworkAccessManager的子类,并将其与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实例上设置以下内容,或者创建QWebView的子类:

QWebView.page().setNetworkAccessManager(myNetworkAccessManager)

Hope this helps! 希望这可以帮助!

Is this question about web filtering? 这是关于网页过滤的问题吗?

Then try use some of external web-proxy, for sample Privoxy ( http://en.wikipedia.org/wiki/Privoxy ). 然后尝试使用一些外部Web代理,用于示例Privoxy( http://en.wikipedia.org/wiki/Privoxy )。

The easylist.txt file is simply plain text, as demonstrated here: http://adblockplus.mozdev.org/easylist/easylist.txt easylist.txt文件只是纯文本,如下所示: 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. 似乎是注释,所以它只是对文件进行排序,并根据easylist.txt文件中该行的起始字符在url / request中搜索正确的内容。

Privoxy is solid. Privoxy很稳固。 If you want it to be completely API based though, check out the BrightCloud web filtering API as well. 如果您希望它完全基于API,请查看BrightCloud Web过滤API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM