Jsoup Parser如何从特定类中获取文本

时间:2017-01-03 10:55:52

标签: java css-selectors jsoup

我需要从下面的标记中提取文本76Houses, Apartments for Rent in。如何使用Jsoup选择器执行此操作?

我试过

Elements elem=doc.select("h1Title >span.id");
String Pagetitle=elem.text();

但它没有用。

<div>
  <h1 class = "h1Title">
    <span id='resultsCount'>76</span> Houses, Apartments for Rent in <span id='loc'>Koramangala</span>
  </h1>
</div>

1 个答案:

答案 0 :(得分:1)

这将为您提供一切:

Elements elem=doc.select(".h1Title");
String pageTitle = elem.text(); //76 Houses, Apartments for Rent in Koramangala

如果您只想要数字:

Elements elem=doc.select(".h1Title #resultsCount");
String pageTitle = elem.text(); // 76

请注意您的所作所为(如果需要,请阅读有关CSS选择器的更多信息):

h1Title > span.id
element h1Title (for class, put a dot first)
this would work if you had <h1Title>
element span : ok (you have <span>)
class id! the . means class, you would have <span class="id">
for id, use # (see my example)

简而言之:

  • foo:tag
  • .foo:class
  • #foo:id