Last active
June 23, 2016 01:18
-
-
Save chaoticsmol/b4ce52048cdd14e35bb77bc015e3867e to your computer and use it in GitHub Desktop.
A python script to stick it to the man
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
import sys | |
import urllib | |
import webbrowser | |
urljoin = None | |
try: | |
# Python 2 | |
import urlparse | |
urljoin = urlparse.urljoin | |
except ImportError: | |
# Python 3 | |
urljoin = urllib.parse.urljoin | |
try: | |
import bs4 | |
except ImportError: | |
print('Please install Beautiful soup by running `pip install beautifulsoup`') | |
sys.exit(1) | |
def replace_resource(source, tag, getter, **attrs): | |
for tag in source.find_all(tag, **attrs): | |
attribute = tag.get(getter, None) | |
if attribute is None: | |
continue | |
abs_path = urljoin(sys.argv[1], tag[getter]) | |
local_path = '/tmp/' + abs_path.split('/')[-1] | |
tag[getter] = 'file://' + local_path | |
f = open(local_path, 'w') | |
f.write(urllib.urlopen(abs_path).read()) | |
f.close() | |
return source | |
source = bs4.BeautifulSoup(urllib.urlopen(sys.argv[1]).read(), 'html.parser') | |
source = replace_resource(source, 'link', 'href', rel='stylesheet') | |
source = replace_resource(source, 'img', 'src') | |
f = open('/tmp/fuckoffwithyourads.html', 'w') | |
f.write(str(source)) | |
f.close() | |
webbrowser.open_new_tab('file:///tmp/fuckoffwithyourads.html') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment