1. 统计区块与表格单元格
如何使用
语义化 HTML 页面 — 下载指定卡片并保留文件名,在本地使用下方共享的静态 HTMLParser 辅助代码分别运行 Python 3 片段,不调用浏览器、脚本、表单地址或样式资源。
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))
结果
语义页面有一个 header、一个 main、两个 section、一个 footer。列表有三项;表格有两个表头单元格、两个数据行及四个数据单元格,不能混淆表头与表体。
2. 匹配标签和表单控件
如何使用
HTML 表单示例 — 下载指定卡片并保留文件名,在本地使用下方共享的静态 HTMLParser 辅助代码分别运行 Python 3 片段,不调用浏览器、脚本、表单地址或样式资源。
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))
结果
四个 label 的 for 值对应四个不同控件 ID:两个 input、一个 select、一个 textarea。只有两个 input 带 required;select 有两个选项。这仅确认声明,不测试提交或浏览器校验。
3. 区分嵌入式 CSS 和行内属性
如何使用
带 CSS 的落地页 — 下载指定卡片并保留文件名,在本地使用下方共享的静态 HTMLParser 辅助代码分别运行 Python 3 片段,不调用浏览器、脚本、表单地址或样式资源。
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))
结果
落地页有一个 style 元素,零个 style 属性。features 区块内有三个 card div。CSS 声明 display: grid 和 repeat(auto-fit, minmax(200px, 1fr)),不能据此断定固定显示列数。