我想知道Jsoup中是否有任何方法可以区分具有相同类的多个元素。为了澄清,请考虑以下HTML片段,我需要检索类名“description”,但我需要区分一个信息与另一个。
<Div class = "related-box gray-text no-margin">
<H3 class = "epsilon"> Awards </ h3>
<P class = "description">
<Strong> Sena - 6 </ strong> <br>
There was no
</ P>
<P class = "description">
<Strong> Quina - 5 </ strong> <br>
124 winning bets, R $ 43,174.39
</ P>
<P class = "description">
<Strong> Quadra - 4 </ strong> <br>
8817 winning bets,
</ P>
谢谢!
答案 0 :(得分:0)
假设您想要在发布的div中获取第二个<p>
标记,您有多个选项。以下示例使用nth-of-type CSS selector,jsoup Elements的get函数(继承自ArrayList)并迭代元素,如果您需要更详细的元素内容比较。还有很多选项,正如我在上面的评论中提到的,使用chrome开发人员工具之类的工具,您可以选择元素并获得匹配的选择器,这可能是一个很好的推广起点。
示例代码
String source ="<div class='related-box gray-text no-margin'>"+
"<h3 class='epsilon'>Awards</h3>"
+ "<p class='description'><strong>Sena - 6</strong><br>There was no</p>"
+ "<p class='description'><strong>Quina - 5</strong><br>124 winning bets, R $ 43,174.39</p>"
+ "<p class='description'><strong>Quadra - 4</strong><br>8817 winning bets,</p></div>";
Document doc = Jsoup.parse(source, "UTF-8");
// nth-of-type(n) CSS selector
Element quina = doc.select(".description:nth-of-type(2)").first();
System.out.println(quina.text());
// Elements.get(n) jsoup method
quina = doc.select(".description").get(1);
System.out.println(quina.text());
// iterate over Elements
Elements descriptions = doc.select(".description");
for (Element element : descriptions) {
if(element.text().contains("Quina")){
quina = element;
}
}
System.out.println(quina.text());
<强>输出强>
Quina - 5 124 winning bets, R $ 43,174.39
Quina - 5 124 winning bets, R $ 43,174.39
Quina - 5 124 winning bets, R $ 43,174.39