我正在尝试使用Jsoup解析html文档以获取所有标题标记.另外我需要将标题标签分组为[h1] [h2]等...
hh = doc.select("h[0-6]");
Run Code Online (Sandbox Code Playgroud)
但这给了我一个空阵列.
oll*_*llo 22
您的选择器意味着h-Tag在此处具有属性"0-6" - 而不是正则表达式.但是你可以组合多个选择器:hh = doc.select("h0, h1, h2, h3, h4, h5, h6");.
分组:您是否需要一个包含所有h-Tags +组的组,每个h1,h2,...标签或每个h1,h2,...标签只有一个组?
以下是如何执行此操作的示例:
// Group of all h-Tags
Elements hTags = doc.select("h1, h2, h3, h4, h5, h6");
// Group of all h1-Tags
Elements h1Tags = hTags.select("h1");
// Group of all h2-Tags
Elements h2Tags = hTags.select("h2");
// ... etc.
Run Code Online (Sandbox Code Playgroud)
如果你想为每个H1组,H2,...标签则可以删除第一选择和替换hTags用doc在其他人.