Skip to content

Instantly share code, notes, and snippets.

@roma-guru
Created September 1, 2017 04:58
Show Gist options
  • Save roma-guru/2f3b57e1a7da9fd9d82daa12cc3a1687 to your computer and use it in GitHub Desktop.
Save roma-guru/2f3b57e1a7da9fd9d82daa12cc3a1687 to your computer and use it in GitHub Desktop.
Simple Python script to download favicons from list of domains (first argument). Used for custom Keepass entry icons.
import sys
import bs4
import requests
from urllib.parse import urlparse, urlunparse
def find_icon(domain):
resp = requests.get("http://{}/".format(domain))
page = bs4.BeautifulSoup(resp.text, 'html.parser')
res = "http://{}/favicon.ico".format(domain)
icons = [e for e in page.find_all(name='link') if 'icon' in e.attrs.get('rel')]
if icons:
res = icons[0].attrs.get('href')
url = urlparse(res, scheme='http')
if not url.netloc:
res = urlunparse((url.scheme, domain, url.path, '', '', ''))
return res
def download(domain, icon_url):
i = icon_url.find('.', len(icon_url)-4)
if i>=0:
ext = icon_url[i+1:]
else:
ext = 'ico'
fname = "{}.{}".format(domain, ext)
resp = requests.get(icon_url)
with open(fname, 'wb') as out:
out.write(resp.content)
if __name__=='__main__':
if len(sys.argv)<2:
print('Need txt file!')
sys.exit()
sites = sys.argv[1]
with open(sites, 'r') as sites:
for d in sites.readlines():
d = d.strip()
download(d, find_icon(d))
requests
BeautifulSoup4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment