Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

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.

Easy Python 3.9+ Jun 28, 2026 Automation & scripting 2 views 0 copies

Requires third-party packages — install first
pip install requests

Python code

28 lines
Python 3.9+
import 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

stdout
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

  1. Parse the HTML <link rel="icon"> tag with BeautifulSoup to find the exact favicon URL.
  2. 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

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Automation & scripting

Related tutorials and quizzes for this topic.