Spaces:
Sleeping
Sleeping
File size: 8,621 Bytes
ee6e247 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | #!/usr/bin/env python3
"""
Parse HTML and extract SEO-relevant elements.
Usage:
python parse_html.py page.html
python parse_html.py --url https://example.com
"""
import argparse
import json
import os
import re
import sys
from typing import Optional
from urllib.parse import urljoin, urlparse
try:
from bs4 import BeautifulSoup
except ImportError:
print("Error: beautifulsoup4 required. Install with: pip install beautifulsoup4")
sys.exit(1)
try:
import lxml # noqa: F401
_HTML_PARSER = "lxml"
except ImportError:
_HTML_PARSER = "html.parser"
# Lazy-loader detection — covers native + the major JS lazy-loaders found on
# WordPress/WooCommerce sites (Perfmatters, EWWW Image Optimizer, generic
# `data-src` patterns). Sites optimized by these plugins strip native
# `loading="lazy"` and replace `src` with a placeholder, so a check on `loading`
# alone reports "not lazy-loaded" when the page is in fact heavily lazy-loaded.
_PERFMATTERS_ATTRS = ("data-perfmatters-src", "data-perfmatters-srcset")
_EWWW_ATTRS = ("data-ewww-src", "data-eio")
_GENERIC_LAZY_ATTRS = ("data-src", "data-lazy-src", "data-original", "data-srcset")
_PERFMATTERS_CLASSES = {"perfmatters-lazy", "perfmatters-lazy-loaded"}
_EWWW_CLASSES = {"lazyload-eio", "lazyloaded-eio"}
_GENERIC_LAZY_CLASSES = {"lazyload", "lazyloaded", "lazy", "lazy-loaded"}
def _detect_lazy_method(img) -> str:
"""Return a coarse classification of the image's lazy-loading mechanism.
Order of detection: native -> perfmatters -> ewww -> js-generic -> none.
Specific JS lazy-loaders are checked before the generic bucket so reports
can attribute the optimization to the right plugin (which informs whether
a site is using a specific WP optimization stack).
Returns one of: 'native', 'perfmatters', 'ewww', 'js-generic', 'none'.
"""
if img.get("loading", "").lower() == "lazy":
return "native"
class_list = set(img.get("class", []) or [])
if any(img.get(a) for a in _PERFMATTERS_ATTRS) or class_list & _PERFMATTERS_CLASSES:
return "perfmatters"
if any(img.get(a) for a in _EWWW_ATTRS) or class_list & _EWWW_CLASSES:
return "ewww"
if any(img.get(a) for a in _GENERIC_LAZY_ATTRS) or class_list & _GENERIC_LAZY_CLASSES:
return "js-generic"
return "none"
def parse_html(html: str, base_url: Optional[str] = None) -> dict:
"""
Parse HTML and extract SEO-relevant elements.
Args:
html: HTML content to parse
base_url: Base URL for resolving relative links
Returns:
Dictionary with extracted SEO data
"""
soup = BeautifulSoup(html, _HTML_PARSER)
result = {
"title": None,
"meta_description": None,
"meta_robots": None,
"canonical": None,
"h1": [],
"h2": [],
"h3": [],
"images": [],
"links": {
"internal": [],
"external": [],
},
"schema": [],
"open_graph": {},
"twitter_card": {},
"word_count": 0,
"hreflang": [],
}
# Title
title_tag = soup.find("title")
if title_tag:
result["title"] = title_tag.get_text(strip=True)
# Meta tags
for meta in soup.find_all("meta"):
name = meta.get("name", "").lower()
property_attr = meta.get("property", "").lower()
content = meta.get("content", "")
if name == "description":
result["meta_description"] = content
elif name == "robots":
result["meta_robots"] = content
# Open Graph
if property_attr.startswith("og:"):
result["open_graph"][property_attr] = content
# Twitter Card
if name.startswith("twitter:"):
result["twitter_card"][name] = content
# Canonical
canonical = soup.find("link", rel="canonical")
if canonical:
result["canonical"] = canonical.get("href")
# Hreflang
for link in soup.find_all("link", rel="alternate"):
hreflang = link.get("hreflang")
if hreflang:
result["hreflang"].append({
"lang": hreflang,
"href": link.get("href"),
})
# Headings
for tag in ["h1", "h2", "h3"]:
for heading in soup.find_all(tag):
text = heading.get_text(strip=True)
if text:
result[tag].append(text)
# Flag suspiciously short or purely numeric headings (likely counters/stats)
stripped = text.strip()
is_suspicious = (
len(stripped) <= 3
or stripped.replace(",", "").replace(".", "").replace("+", "").replace("-", "").replace("%", "").replace(" ", "").isdigit()
)
if is_suspicious:
key = f"{tag}_suspicious"
if key not in result:
result[key] = []
result[key].append(text)
# Images
for img in soup.find_all("img"):
src = img.get("src", "")
if base_url and src:
src = urljoin(base_url, src)
result["images"].append({
"src": src,
"alt": img.get("alt"),
"width": img.get("width"),
"height": img.get("height"),
"loading": img.get("loading"),
"lazy_method": _detect_lazy_method(img),
})
# Links
if base_url:
base_domain = urlparse(base_url).netloc
for a in soup.find_all("a", href=True):
href = a.get("href", "")
if not href or href.startswith("#") or href.startswith("javascript:"):
continue
full_url = urljoin(base_url, href)
parsed = urlparse(full_url)
link_data = {
"href": full_url,
"text": a.get_text(strip=True)[:100],
"rel": a.get("rel", []),
}
if parsed.netloc == base_domain:
result["links"]["internal"].append(link_data)
else:
result["links"]["external"].append(link_data)
# Schema (JSON-LD)
for script in soup.find_all("script", type="application/ld+json"):
try:
schema_data = json.loads(script.string)
# Flatten @graph containers so each @type is a separate entry
if isinstance(schema_data, dict) and "@graph" in schema_data:
for item in schema_data["@graph"]:
if isinstance(item, dict):
result["schema"].append(item)
elif isinstance(schema_data, list):
for item in schema_data:
if isinstance(item, dict):
result["schema"].append(item)
else:
result["schema"].append(schema_data)
except (json.JSONDecodeError, TypeError):
pass
# Word count (visible text only)
for element in soup(["script", "style", "nav", "footer", "header"]):
element.decompose()
text = soup.get_text(separator=" ", strip=True)
words = re.findall(r"\b\w+\b", text)
result["word_count"] = len(words)
return result
def main():
parser = argparse.ArgumentParser(description="Parse HTML for SEO analysis")
parser.add_argument("file", nargs="?", help="HTML file to parse")
parser.add_argument("--url", "-u", help="Base URL for resolving links")
parser.add_argument("--json", "-j", action="store_true", help="Output as JSON")
args = parser.parse_args()
if args.file:
real_path = os.path.realpath(args.file)
if not os.path.isfile(real_path):
print(f"Error: File not found: {args.file}", file=sys.stderr)
sys.exit(1)
with open(real_path, "r", encoding="utf-8") as f:
html = f.read()
else:
html = sys.stdin.read()
result = parse_html(html, args.url)
if args.json:
print(json.dumps(result, indent=2))
else:
print(f"Title: {result['title']}")
print(f"Meta Description: {result['meta_description']}")
print(f"Canonical: {result['canonical']}")
print(f"H1 Tags: {len(result['h1'])}")
print(f"H2 Tags: {len(result['h2'])}")
print(f"Images: {len(result['images'])}")
print(f"Internal Links: {len(result['links']['internal'])}")
print(f"External Links: {len(result['links']['external'])}")
print(f"Schema Blocks: {len(result['schema'])}")
print(f"Word Count: {result['word_count']}")
if __name__ == "__main__":
main()
|