How could you block using Python?

I am slowly building a web browser in PyQt4 and like the speed that I am exiting from it. However, I want to combine easylist.txt with it. I believe adblock uses this to block HTTP requests from the browser.

How would you do this using python / PyQt4?

[edit1] OK. I think I configured Privoxy. I do not configure any additional filters and it seems to work. PyQt4, which I 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 nothing, and I cannot understand the documents and cannot find any examples.

[edit2] I just noticed that if I change self.proxyIP to my local IP address and not to 127.0.0.1, the page does not load. So something is happening.

+4
source share
4 answers

I know this is an old question, but I thought that I would try to give an answer to everyone who accidentally stumbles upon it. You can subclass QNetworkAccessManager and merge it with https://github.com/atereshkin/abpy . Something like that:

 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 for all QWebView instances or subclass QWebView:

 QWebView.page().setNetworkAccessManager(myNetworkAccessManager) 

Hope this helps!

+4
source

Question about web filtering?

Then try using some external web proxy, for example Privoxy ( http://en.wikipedia.org/wiki/Privoxy ).

0
source

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

lines starting with [as well! seem to be comments, so this is just a case of sorting by file and finding the right things in url / request depending on the starting character of the line in easylist.txt.

0
source

Privoxy is solid. If you want it to be entirely API based, check out the BrightCloud web filter .

0
source

All Articles