-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_icon.py
29 lines (27 loc) · 853 Bytes
/
get_icon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import urllib.request
from urllib.error import URLError, HTTPError
from urllib.parse import urlparse
"""
This function returns the favicon of a webpage. (Stored in /images)
"""
def get_icon(url):
#Create favicon path
scheme, netloc, path, params, query, fragment = urlparse(url)
link = scheme + '://' + netloc + '/favicon.ico'
site_name = netloc.split('.')
#Name image
if site_name[0] == 'www':
name = site_name[1]
else:
name = site_name[0]
path = 'images/' + name + '.png'
try:
local_filename, headers = urllib.request.urlretrieve(link, path)
#If icon is not found, return default icon
except HTTPError as e:
icon_path = 'images/default.png'
except URLError as e:
icon_path = 'images/default.png'
else:
icon_path = local_filename
return icon_path