Python
Beginner
Estimated Duration: 15 min
Python: Extracting SEO Meta Tags from HTML
Learn how to parse HTML strings and extract the title, meta description, and primary H1 tag using Python regex.
Understanding On-Page Meta Tag Extraction
Search engine crawlers (Googlebot, Bingbot) scan raw HTML documents to extract critical indexing signals:
1. **Title Tag** (
<title>): The clickable headline in search results.2. **Meta Description** (
<meta name="description" content="...">): The summary snippet.3. **Primary Heading** (
<h1>): The central topical topic of the webpage.In this lesson, you will write a clean Python parser that extracts these three essential fields from raw HTML source code.
#
Key Principles:
- Use non-greedy regex matching (.*?) to avoid capturing across multiple tags.- Use
re.IGNORECASE because HTML tag names are case-insensitive.- Strip leading and trailing whitespace from extracted values.
Target Output
{'title': 'Seoffix - Free SEO Site Auditor & Tools', 'description': 'Audit your website with 236 automated SEO checkpoints.', 'h1': 'Complete SEO Site Audit Engine'}
Need a Hint?
- Use re.search(r'<title[^>]*>(.*?)</title>', html, re.I)
- For meta description, look for name='description' and capture content='...'
- Use .strip() to clean whitespace from the matched group
solution.py
Terminal Output
Ready
Click "Run Code & Test" to execute your solution.