1. Count sections and table cells
How to use
Semantic HTML Page — Download the named card and keep its filename. Run each Python 3 snippet locally with the shared inert HTMLParser helper below; no browser, script, form action or stylesheet URL is invoked.
from pathlib import Path
from html.parser import HTMLParser
class Inspect(HTMLParser):
def __init__(self):
super().__init__(); self.nodes=[]; self.stack=[]; self.css=[]
def handle_starttag(self, tag, attrs):
attrs=dict(attrs)
self.nodes.append((tag,attrs,tuple(self.stack)))
if tag not in {"meta","input","link","br","hr","img"}: self.stack.append(tag)
def handle_endtag(self, tag):
if tag in self.stack:
self.stack=self.stack[:len(self.stack)-1-self.stack[::-1].index(tag)]
def handle_data(self,data):
if self.stack and self.stack[-1]=="style": self.css.append(data)
def inspect(filename):
with Path(filename).open("rb") as f: b=f.read(10001)
assert len(b)<=10000
p=Inspect();p.feed(b.decode("utf-8"));p.close();return p
p=inspect("simple.html")
count=lambda t:sum(tag==t for tag,attrs,parents in p.nodes)
print([count(t) for t in ("header","main","section","footer","li","th","td")])
print(sum(tag=="tr" and "tbody" in parents for tag,attrs,parents in p.nodes))
Outcome
The semantic page has one header, one main, two sections and one footer. Its list has three items; the table has two header cells and two data rows with four data cells. Header and body rows are not interchangeable.
2. Match labels to form controls
How to use
HTML Form Example — Download the named card and keep its filename. Run each Python 3 snippet locally with the shared inert HTMLParser helper below; no browser, script, form action or stylesheet URL is invoked.
from pathlib import Path
from html.parser import HTMLParser
class Inspect(HTMLParser):
def __init__(self):
super().__init__(); self.nodes=[]; self.stack=[]; self.css=[]
def handle_starttag(self, tag, attrs):
attrs=dict(attrs)
self.nodes.append((tag,attrs,tuple(self.stack)))
if tag not in {"meta","input","link","br","hr","img"}: self.stack.append(tag)
def handle_endtag(self, tag):
if tag in self.stack:
self.stack=self.stack[:len(self.stack)-1-self.stack[::-1].index(tag)]
def handle_data(self,data):
if self.stack and self.stack[-1]=="style": self.css.append(data)
def inspect(filename):
with Path(filename).open("rb") as f: b=f.read(10001)
assert len(b)<=10000
p=Inspect();p.feed(b.decode("utf-8"));p.close();return p
p=inspect("form-example.html")
labels=[a["for"] for t,a,_ in p.nodes if t=="label"]
controls=[(t,a) for t,a,_ in p.nodes if t in ("input","select","textarea")]
assert set(labels)=={a["id"] for t,a in controls} and len(set(labels))==4
assert [t for t,a in controls if "required" in a]==["input","input"]
print(len(labels),len(controls),sum(t=="option" for t,a,_ in p.nodes))
Outcome
Four label for-values match four distinct control IDs: two inputs, one select and one textarea. Only the two input elements carry required; the select has two options. This confirms declarations, not submission or browser validation.
3. Distinguish embedded CSS from inline attributes
How to use
Landing Page with CSS — Download the named card and keep its filename. Run each Python 3 snippet locally with the shared inert HTMLParser helper below; no browser, script, form action or stylesheet URL is invoked.
from pathlib import Path
from html.parser import HTMLParser
class Inspect(HTMLParser):
def __init__(self):
super().__init__(); self.nodes=[]; self.stack=[]; self.css=[]
def handle_starttag(self, tag, attrs):
attrs=dict(attrs)
self.nodes.append((tag,attrs,tuple(self.stack)))
if tag not in {"meta","input","link","br","hr","img"}: self.stack.append(tag)
def handle_endtag(self, tag):
if tag in self.stack:
self.stack=self.stack[:len(self.stack)-1-self.stack[::-1].index(tag)]
def handle_data(self,data):
if self.stack and self.stack[-1]=="style": self.css.append(data)
def inspect(filename):
with Path(filename).open("rb") as f: b=f.read(10001)
assert len(b)<=10000
p=Inspect();p.feed(b.decode("utf-8"));p.close();return p
p=inspect("landing-page.html")
assert sum(t=="style" for t,a,_ in p.nodes)==1
assert sum("style" in a for t,a,_ in p.nodes)==0
cards=[a for t,a,parents in p.nodes if t=="div" and a.get("class")=="card" and "section" in parents]
css="".join(p.css)
assert "display: grid" in css and "repeat(auto-fit, minmax(200px, 1fr))" in css
print(1,0,len(cards))
Outcome
The landing page has one style element and zero style attributes. Three card divs sit inside the features section. The embedded CSS declares display: grid and repeat(auto-fit, minmax(200px, 1fr)); it does not establish a fixed number of rendered columns.