在获取我尝试使用
的html内容后,我使用MediaWiki API获取了Wikipedia页面。p:not(h2 ~ p)
要获取页面摘要段落,它应该是目录元素之前的段落,它获取了所需的部分,但还有其他段落,这是哪里出了问题?
答案 0 :(得分:1)
p:not(h2 ~ p)
在同一父级中获得页面上每个没有h2
的段落。这包括嵌套的段落,主要内容之外的段落等,因为这些段落中没有一个与h2
本身共享相同的父元素。你不想要那些您只希望在其父元素中的h2
元素之前出现这些段落。
为此,您希望将外部p
选择器锚定到父元素。您想要的父元素是.mw-parser-output
:
.mw-parser-output > p:not(h2 ~ p)
答案 1 :(得分:1)
代码:
public static void main(String[] args){
Document doc = null;
String url = "https://en.wikipedia.org/wiki/Nico_Ditch";
try {
doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
} catch (IOException e) {
e.printStackTrace();
}
Elements els = doc.select(".mw-parser-output > p:not(h2 ~ p)");
System.out.println(els);
// System.out.println(doc);
}
运行输出:
<p class="mw-empty-elt"> </p>
<p><b>Nico Ditch</b> is a six-mile (9.7 km) long linear <a href="/wiki/Earthworks_(archaeology)" title="Earthworks (archaeology)">earthwork</a> between <a href="/wiki/Ashton-under-Lyne" title="Ashton-under-Lyne">Ashton-under-Lyne</a> and <a href="/wiki/Stretford" title="Stretford">Stretford</a> in Greater Manchester, England. It was dug as a defensive fortification, or possibly a boundary marker, between the 5th and 11th centuries. </p>
<p>The ditch is still visible in short sections, such as a 330-yard (300 m) stretch in <a href="/wiki/Denton,_Greater_Manchester" title="Denton, Greater Manchester">Denton</a> Golf Course. In the parts which survive, the ditch is 4–5 yards (3.7–4.6 m) wide and up to 5 feet (1.5 m) deep. Part of the earthwork is protected as a <a href="/wiki/Scheduled_Ancient_Monument" class="mw-redirect" title="Scheduled Ancient Monument">Scheduled Ancient Monument</a>. </p>
以退出代码0结束的过程