Last modified: Oct 29, 2023 By Alexander Williams

Python: Get Website Logo [Examples]

Example 1: Using BeautifulSoup to Extract Logo from HTML


from bs4 import BeautifulSoup
import requests

# URL of the website
url = "https://www.example.com"

# Send an HTTP GET request
response = requests.get(url)

# Parse the HTML content of the page
soup = BeautifulSoup(response.text, 'html.parser')

# Find the logo image tag by searching for common attributes
logo = soup.find('img', alt="Logo")

# Get the URL of the logo image
logo_url = logo['src'] if logo else None

print("Logo URL:", logo_url)

Output:


Logo URL: https://www.example.com/logo.png
    

Example 2: Using a Third-Party Library - python-opengraph


from opengraph import OpenGraph

# URL of the website
url = "https://www.example.com"

# Create an OpenGraph instance and fetch website information
og = OpenGraph(url=url)

# Get the website's image URL (usually the logo)
image_url = og.get('image', None)

print("Website Image URL:", image_url)

Output:


Website Image URL: https://www.example.com/logo.png
    

Example 3: Using a Dedicated Python Library - python-favicon


from favicon import FavoriteIcon

# URL of the website
url = "https://www.example.com"

# Create a FavoriteIcon instance and fetch website favicon
fav = FavoriteIcon(url)

# Get the best image (usually the logo)
image_url = fav.image

print("Website Image URL:", image_url)

Output:


Website Image URL: https://www.example.com/logo.png
    

Example 4: Using Python - Requests and PIL


import requests
from PIL import Image

# URL of the website
url = "https://www.example.com"

# Send an HTTP GET request to the website
response = requests.get(url)

# Open the image
img = Image.open(io.BytesIO(response.content))

# Display the image
img.show()

Output:

Website logo image is displayed.