Analyzing Web Pages and Improving SEO with Python

☕️ 4 minutes


Analyzing Webpages with Python: A Practical Guide


In today's digital world, webpages play a crucial role in providing information, services, and engaging experiences to users. As a developer, designer, or SEO specialist, it is essential to analyze webpages to understand their structure, content, and optimization opportunities. By gaining insights into the various aspects of a webpage, you can make informed decisions to improve its performance, user experience, and visibility in search engines.

Webpage analysis involves examining different elements, such as structured data, images, metadata, heading structure, hreflang tags, internal links, and keyword usage. Each of these aspects provides valuable information about the webpage's organization, relevance, accessibility, and SEO optimization.

In this blog post, we will explore a practical guide to analyzing webpages using the power of Python. Python is a versatile programming language with a rich ecosystem of libraries and tools that make web scraping, data extraction, and analysis efficient and straightforward. By leveraging Python's capabilities, we can automate the process of webpage analysis, save time, and extract valuable insights.

Throughout this guide, we will walk through a Python code snippet that demonstrates how to analyze webpages effectively. We will utilize popular libraries like BeautifulSoup, json, and NLTK to extract and process various elements of a webpage. The code snippet will cover a range of analysis techniques, including structured data analysis, image analysis, metadata extraction, heading structure analysis, hreflang tag evaluation, internal link examination, and keyword analysis.

By following along with the code snippet and explanations, you will learn how to extract structured data in JSON format, analyze images for size and optimization opportunities, extract metadata such as page title and description, evaluate the heading structure for content organization, examine hreflang tags for international SEO considerations, explore internal links for website navigation, and analyze keywords to understand the main topics covered on a webpage.

Whether you are a web developer seeking to enhance website performance, an SEO specialist aiming to optimize search engine visibility, or simply a curious learner interested in understanding webpage analysis techniques, this practical guide will provide you with the knowledge and tools to analyze webpages effectively using Python.

Now, let's dive into the code and explore how Python can assist us in gaining valuable insights from webpages!

Analyzing Structured Data

Structured data plays a crucial role in modern web development and search engine optimization. It provides search engines with organized and semantically meaningful information about the content on a webpage. By analyzing structured data, we can gain insights into the type of information the webpage contains, improve its visibility in search engine results, and enhance the user experience.

In this section, we will focus on analyzing structured data using Python. We will leverage the BeautifulSoup library, which is a powerful tool for parsing HTML and XML documents, to extract structured data from webpages.

To begin, we need to identify the presence of structured data on a webpage. Structured data is typically implemented using markup formats like JSON-LD, Microdata, or RDFa. These formats allow us to define entities, properties, and their values in a structured and machine-readable manner.

Once we have identified the structured data on a webpage, we can extract it using BeautifulSoup. The library provides various methods to navigate and search the HTML structure, making it easy to locate and extract the structured data elements.

After extracting the structured data, we can analyze its contents. For example, we can determine the type of structured data being used, such as types like "Article," "Product," or "Organization." This information helps us understand the purpose and context of the webpage's content.

def analyze_structured_data(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    script_tags = soup.find_all("script", type="application/ld+json")
    structured_data = []
    for tag in script_tags:
            data = json.loads(tag.string)
        except json.JSONDecodeError:
    return structured_data
# Analyze structured data on the webpage
structured_data = analyze_structured_data(url)

Furthermore, we can extract specific properties and their corresponding values from the structured data. These properties can include the article's title, author, publication date, image, description, or any other relevant information. By analyzing these properties, we can evaluate the completeness and accuracy of the structured data implementation.

Another aspect of structured data analysis is verifying the validity and adherence to schema guidelines. Various schemas have specific requirements and recommendations for implementing structured data. By validating the structured data against the schema guidelines, we can identify potential issues or opportunities for improvement.

In addition to analyzing the structured data on a single webpage, we can also compare it to the structured data on other pages within the same website or across different websites. This allows us to gain insights into how the website utilizes structured data consistently and identify any discrepancies or areas for improvement.

By analyzing structured data using Python, we can automate the process of extracting, evaluating, and comparing structured data across multiple webpages. This automation saves time and ensures consistent analysis, making it easier to identify optimization opportunities and enhance the webpage's visibility and user experience.

Image Analysis

Images are a fundamental component of webpages and play a crucial role in enhancing user engagement and visual appeal. Analyzing images on a webpage allows us to gain insights into their characteristics, optimize them for better performance, and improve the overall user experience.

Image Metadata Extraction

One aspect of image analysis is extracting metadata associated with images. Metadata includes information such as image dimensions, file size, image format (JPEG, PNG, GIF, etc.), color space, and creation date. By extracting and analyzing this metadata, we can gain insights into the technical aspects of the images on a webpage.

Using the PIL library in Python, we can easily extract image metadata. The library provides functions to access properties like image size, format, and creation date. By examining this metadata, we can identify potential issues, such as oversized images or outdated formats, and take appropriate actions to optimize them.

Image Classification and Object Recognition

Image classification and object recognition involve the identification and categorization of objects within an image. This analysis technique is useful in various applications, such as content filtering, visual search, and automated tagging.

With the help of deep learning frameworks like TensorFlow or Keras, we can build and train image classification models. These models can learn to recognize specific objects, animals, or scenes based on a large labeled dataset. By applying these models to analyze images on a webpage, we can automatically classify and identify the objects present in each image.

Image Quality Assessment

Image quality plays a crucial role in user experience. High-quality images are visually appealing and can positively impact engagement, while low-quality images may result in a negative perception of the webpage. Image quality assessment techniques help us evaluate the visual quality of images objectively.

Tools like OpenCV provide algorithms to measure image quality metrics such as sharpness, noise level, contrast, and color saturation. By applying these algorithms, we can assess the quality of images on a webpage and identify any issues that may require optimization or replacement.

def analyze_images(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, "html.parser")
    image_tags = soup.find_all("img")
    image_analysis = []
    for tag in image_tags:
        image_src = tag.get("src", "")
        if image_src.startswith("https:"):
            image_alt = tag.get("alt", "")
            image_size = get_image_size(image_src)
            image_optimization = optimize_image(image_size)
            image_data = {
                "src": image_src,
                "alt": image_alt,
                "size": image_size,
                "optimized": image_optimization,
    return image_analysis
def get_image_size(image_url):
    response = requests.head(image_url)
    size_header = response.headers.get("content-length")
    if size_header:
        size_bytes = int(size_header)
        size_kb = size_bytes / 1024
        size_mb = size_kb / 1024
        return f"{size_mb:.2f} MB" if size_mb > 1 else f"{size_kb:.2f} KB"
    return "Unknown"
def optimize_image(image_size):
    # Add your image optimization logic here
    # You can define thresholds or criteria for optimization based on image sizes
    # For example, you can check if image_size exceeds a certain limit and mark it as "Not Optimized"        return "Too Big"
    return "Optimized"  # Replace with your logic
# Analyze images on the webpage
image_analysis = analyze_images(url)

Image Compression and Optimization

Optimizing image file sizes is essential for improving webpage load times and reducing bandwidth usage. Image compression techniques allow us to reduce the file size of images without significant loss in visual quality.

Python libraries like Pillow provide functions to compress and optimize images. These libraries offer various compression algorithms, such as JPEG compression, that balance file size reduction and visual quality. By applying image compression techniques, we can reduce the file sizes of images on a webpage, resulting in faster load times and improved user experience.

Image Accessibility Analysis

Web accessibility is a crucial aspect of inclusive design. Analyzing images for accessibility involves ensuring that alternative text (alt text) is properly provided for images. Alt text describes the content and purpose of an image, enabling users with visual impairments or using assistive technologies to understand the visual context.

Through Python scripts or tools, we can automatically analyze webpages for missing or inadequate alt text for images. By identifying and rectifying these issues, we can ensure that all users, including those with visual impairments, can access and understand the content conveyed through images.

In conclusion, analyzing images on a webpage using Python empowers us to extract metadata, classify objects, assess image quality, compress images, and enhance accessibility. By applying image analysis techniques, we can optimize images for better performance, improve user experience, and create visually compelling webpages.

Image Metadata Extraction

Image metadata refers to the embedded information associated with an image file. This metadata provides valuable insights into the technical aspects and characteristics of an image. Extracting and analyzing image metadata can help in various scenarios, such as optimizing images for web display, organizing and categorizing image collections, and ensuring compliance with image standards and guidelines.

When it comes to image metadata extraction, the Python Imaging Library (PIL) provides a convenient way to access and retrieve metadata properties. The library offers a module called PIL.ExifTags, which allows us to extract metadata specifically from the Exchangeable Image File Format (EXIF). EXIF metadata typically includes details about the camera or device used to capture the image, camera settings (such as aperture, shutter speed, ISO), and other technical information.

from PIL import Image, ExifTags
def extract_exif_metadata(image_path):
    img =
    exif_data = img._getexif()
    if exif_data is not None:
        for tag, value in exif_data.items():
            if tag in ExifTags.TAGS:
                print(f"{ExifTags.TAGS[tag]}: {value}")

In this code snippet, we open the image using and retrieve the EXIF metadata using img._getexif(). The metadata is stored in a dictionary format, where the keys represent the EXIF tags and the values contain the corresponding metadata values. We then iterate over the dictionary and print out the tag names and their respective values.

Besides EXIF metadata, PIL also allows extraction of other image properties, such as the image format, dimensions, color mode, and creation date. Here's an example code snippet to extract some of these properties:

from PIL import Image
def extract_image_properties(image_path):
    img =
    print(f"Format: {img.format}")
    print(f"Dimensions: {img.size[0]} x {img.size[1]}")
    print(f"Color Mode: {img.mode}")
    print(f"Creation Date: {'creation_date')}")

In this code snippet, we open the image using and then access various properties using the format, size, mode, and info attributes of the Image object. The info attribute provides a dictionary that can store additional image-specific metadata, such as the creation date.

By extracting image metadata, we can gain insights into the technical details of images on a webpage. This information can be used to identify potential issues, such as oversized images or outdated formats, and take appropriate actions to optimize them. Additionally, metadata extraction can facilitate organization and categorization of image collections, making it easier to search and manage images effectively.

Overall, image metadata extraction using Python and libraries like PIL enables us to delve into the technical aspects of images, empowering us to make informed decisions regarding image optimization, compliance, and management.

Heading Structure

In the context of document analysis and understanding, the heading structure refers to the organization and hierarchy of headings or titles within a document. Headings play a crucial role in providing a logical and structured representation of the content, making it easier for readers and automated systems to navigate and comprehend the document.

When it comes to analyzing structured data, such as documents, web pages, or even images, understanding the heading structure becomes essential. By analyzing the heading structure, we can gain insights into the organization of information, identify key sections, and extract meaningful content.

One common approach to analyze the heading structure is by leveraging HTML tags in the case of web pages. HTML provides a set of heading tags, ranging from <h1> to <h6>, where <h1> represents the highest level of heading and <h6> represents the lowest. By examining the hierarchical relationships among these tags, we can determine the heading structure.

from bs4 import BeautifulSoup
def analyze_heading_structure(html_content):
    soup = BeautifulSoup(html_content, 'html.parser')
    headings = soup.find_all(['h1', 'h2', 'h3', 'h4', 'h5', 'h6'])
    for heading in headings:
        level = int([1])  # Extract the heading level (1 to 6)
        text = heading.get_text()    # Extract the text content of the heading
        print(f"{'#' * level} {text}")

Apart from HTML documents, heading structure analysis can also be applied to other types of structured data, such as images. While images may not have traditional heading tags, we can still extract metadata that provides information about the image's content and structure.

Here's an example code snippet in Python that demonstrates how to extract image metadata, including the image format, dimensions, and color mode:

from PIL import Image
def extract_image_metadata(image_path):
    img =
    print("Image Metadata:")
    print(f"Format: {img.format}")
    print(f"Dimensions: {img.size[0]} x {img.size[1]}")
    print(f"Color Mode: {img.mode}")

By analyzing the heading structure in documents and extracting metadata from images, we can gain valuable insights and information about the content and structure of the data. These insights can be used for various purposes, such as organizing and categorizing documents, optimizing web pages, or implementing intelligent search and retrieval systems.


Hreflang is an HTML attribute used to specify the language and geographical targeting of a webpage. It is particularly useful for websites with content in multiple languages or targeting specific regions. By using hreflang annotations, search engines can better understand the language and regional targeting of your content, improving the accuracy of search results for users.

  <link rel="alternate" hreflang="en-US" href="" />
  <link rel="alternate" hreflang="es-ES" href="" />

By including hreflang annotations in your webpages, you can help search engines present the appropriate version of your content to users based on their language and geographical preferences.

Internal links are hyperlinks that connect different pages within the same website or domain. They play a crucial role in improving website navigation, user experience, and search engine optimization. Internal linking helps users discover related content, establishes a hierarchical structure for your website, and distributes authority and ranking potential across different pages.

<!DOCTYPE html>
    <title>My Website</title>
    <h1>Welcome to My Website</h1>
    <p>Check out my <a href="about.html">About</a> page!</p>

Internal links can be created between various pages within a website to establish a network of interconnected content. By strategically placing internal links throughout your website, you can guide users to relevant pages, increase engagement, and help search engines discover and crawl your content more effectively.

Implementing hreflang annotations and internal links correctly can have a significant impact on the visibility and accessibility of your website's content. By using these techniques, you can enhance the user experience, target specific audiences, and improve your website's search engine rankings.

Keyword Analysis

Keyword analysis is an essential aspect of search engine optimization (SEO) that involves researching and selecting the right keywords to optimize your website's content. By understanding the keywords and phrases that users commonly search for, you can optimize your content to improve its visibility and relevance in search engine results. Keyword analysis helps you identify the language and terms your target audience uses, enabling you to align your content with their search intent.

Keyword Research

Keyword research involves finding keywords that are relevant to your website's content and have a reasonable search volume.

import pytrends
from pytrends.request import TrendReq
# Set up pytrends
pytrends = TrendReq(hl='en-US', tz=360)
# Define the keyword you want to research
keyword = "digital marketing"
# Build the payload
# Get the interest over time data
interest_over_time = pytrends.interest_over_time()
# Print the interest over time data

By analyzing the interest over time data and related metrics, you can gain insights into the keyword's search volume, seasonality, and overall popularity.

Keyword Optimization

Once you've identified relevant keywords through research, the next step is to optimize your content accordingly. This involves incorporating the selected keywords strategically into your website's pages, titles, headings, meta tags, and other relevant elements. Here's an example code snippet in HTML that demonstrates keyword optimization for a webpage:

<!DOCTYPE html>
    <title>My Website - Digital Marketing</title>
      content="Learn about digital marketing strategies and techniques to boost your online presence."
    <h1>Digital Marketing</h1>
      Welcome to my website! Here, you'll find valuable information about
      various digital marketing strategies.
    <h2>Why Digital Marketing Matters</h2>
      Digital marketing has become essential for businesses to reach their
      target audience in the digital age.

By optimizing your content with relevant keywords, you improve its relevance to search engines and increase the likelihood of your website appearing in search results when users search for those keywords.

Keyword analysis is an ongoing process that requires monitoring and adjusting your keyword strategy based on changes in search trends and user behavior. By continually analyzing and optimizing your content for relevant keywords, you can improve your website's visibility, attract more organic traffic, and reach your target audience effectively.

Leveraging Python Scripts for SEO Marketing

Python scripts can be powerful tools for SEO professionals to enhance their marketing efforts. By using Python, SEO experts can automate tasks, extract valuable data, perform analysis, and optimize various aspects of their websites. Here's a summary of how an SEO professional can utilize Python scripts to improve their marketing strategies:

  1. Automating SEO Tasks: Python allows SEO professionals to automate repetitive tasks, such as website crawling, data scraping, and content generation. By writing scripts, they can save time and effort, enabling them to focus on more strategic aspects of SEO.

  2. Data Extraction and Analysis: Python's rich ecosystem of libraries, such as BeautifulSoup and Scrapy, enables SEO professionals to extract data from websites, analyze it, and gain valuable insights. They can collect data on competitors, keywords, backlinks, and user behavior, which helps in making informed marketing decisions.

  3. Keyword Research and Optimization: Python scripts can assist in keyword research by scraping search engine results, analyzing search trends, and generating keyword suggestions. SEO professionals can use these insights to optimize website content, meta tags, and headings to improve organic rankings and attract targeted traffic.

  4. Technical SEO Optimization: Python scripts can help automate technical SEO tasks, such as checking broken links, validating HTML markup, and optimizing website performance. SEO professionals can write scripts to crawl their websites, identify issues, and generate reports for further analysis and optimization.

  5. Content Analysis and Optimization: Python scripts can analyze content quality, readability, and keyword density. By utilizing Natural Language Processing (NLP) libraries like NLTK, SEO professionals can assess the effectiveness of their content and make data-driven improvements.

  6. Monitoring and Reporting: Python scripts can be used to monitor website performance, track rankings, and generate reports. SEO professionals can automate the process of collecting data, analyzing metrics, and generating visual reports to provide valuable insights to stakeholders.

  7. Automation of Link Building: Python scripts can assist in automating parts of the link building process, such as prospecting for link opportunities, analyzing backlink profiles, and monitoring link quality. SEO professionals can identify authoritative websites and streamline the link building outreach process.

By harnessing the power of Python scripts, SEO professionals can streamline their workflows, gain valuable insights, and optimize various aspects of their marketing strategies. It empowers them to make data-driven decisions, improve website visibility, and achieve better search engine rankings, ultimately driving more organic traffic and improving overall marketing success.

Complete code

The complete code can be found on this GitHub repository.