How to Automatically Download Every Favicon from a List of Websites in Python
Download each website's favicon.ico file by constructing its URL, making a GET request, and saving the binary content locally.
pip install requests
Python code
28 linesimport requests
from urllib.parse import urlparse
import os
websites = [
"https://www.google.com",
"https://www.github.com",
"https://www.stackoverflow.com"
]
def download_favicon(url):
parsed = urlparse(url)
favicon_url = f"{parsed.scheme}://{parsed.netloc}/favicon.ico"
response = requests.get(favicon_url, stream=True)
if response.status_code == 200:
filename = f"{parsed.netloc.replace('.', '_')}_favicon.ico"
with open(filename, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
f.write(chunk)
print(f"Downloaded: {filename} ({len(response.content)} bytes)")
return True
else:
print(f"Failed to download favicon from {url}")
return False
if __name__ == "__main__":
for website in websites:
download_favicon(website)
Output
Downloaded: www_google_com_favicon.ico (15406 bytes)
Downloaded: www_github_com_favicon.ico (2330 bytes)
Downloaded: www_stackoverflow_com_favicon.ico (1150 bytes)
How it works
The script constructs the standard favicon URL using the scheme and netloc from the parsed website URL. It uses requests.get with stream=True for memory-efficient downloading, then writes the response in 8192-byte chunks. The filename is derived from the netloc, replacing dots with underscores to avoid filesystem issues. If a website doesn't return status 200, the script prints a failure message and continues to the next URL.
Common mistakes
- Forgetting that some sites use a non-standard favicon path or CDN-hosted favicon.
- Not handling redirects correctly — `requests` follows them by default but you may get a different file type.
- Assuming every website has a favicon.ico at the root — always check the response status.
- Using `response.content` directly without streaming may cause high memory usage for large favicons.
Variations
- Parse the HTML <link rel="icon"> tag with BeautifulSoup to find the exact favicon URL.
- Convert the favicon to PNG using Pillow after download for consistent format.
Real-world use cases
- Gathering favicons for a dashboard that displays brand logos alongside bookmark links.
- Building a website monitoring tool that saves site assets for offline analysis.
- Creating a favicon cache for a browser extension that shows site icons in search results.
Sponsored
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.