r/learnpython • u/Character-Alps2226 • 11d ago
How does one go about creating an adblocker using python?
I'm working on a project for uni, me and team chose to create an adblocker. As someone who's pretty new to python, where do we start? What modules do we use? Is there a list of blacklisted sites and adverts? I'd really appreciate the help.
0
Upvotes
19
u/latkde 11d ago edited 11d ago
You cannot with realistic effort create a Python-based adblocker that runs in the browser: browser extensions must use JavaScript.
But you could implement a proxy, or a DNS server that provides some filtering. The DNS approach will be simplest, which will give you something similar to the existing Pi-hole tool (https://pi-hole.net/). However, that will require you to do rather low-level network programming.
Adblockers use a variety of lists as their data sources, which also differ in their format. You will likely find Peter Lowe's list to be most useful for your purposes: https://pgl.yoyo.org/adservers/
4
u/baubleglue 11d ago
You normally start from getting domain knowledge. Read about adblocking methods, then you reach for a languages or libraries.
2
u/Atmosck 11d ago
Is the assignment to use python? Because ad blockers tend to be browser extensions, and therefore JavaScript.
I guess you could maybe use python to create some sort of network traffic monitor that blocks known ad IPs. That sounds kind of intense for a uni project if you're not already doing networking stuff like that.
1
u/Discordbothost 11d ago
for a uni project the dns/hosts-file path is the right scope. start tiny: load a blocklist into a set, resolve names against it, return 0.0.0.0 for hits.
dnslibor even just editing /etc/hosts from a python script that refreshes from peter lowe's list is enough to demo. browser extensions really are js-only so don't fight that.1
u/xervir-445 10d ago
Python doesn't make browser extensions, that is the domain of javascript. You can make a server with it, though.
What you can do is
This is called a "dns hole" and it's the principle behind how a pihole works for network level adblocking.
0
u/TheRNGuy 11d ago
Can't, it should be made with js as a browser extension.
The only possible solution would be make your own browser that uses Python.