从班级获取文本

时间:2019-03-13 13:07:39

标签: java jsoup

嗨,我无法从html中获取文本,我想获取此文本This is a test text

<div class="rehou">
       <span class="tlid-t t">
         <span title="" class="">This is a test text</span>
       </span>
    <span class="tlid-t-v" style="" role="button"></span>
</div>

我的java:

Document doc = Jsoup.connect(url).get();
Elements ele= doc.select("span.tlid-t t");
textass = ele.text();

1 个答案:

答案 0 :(得分:1)

span具有两个不同的类tlid-tt。因此,如果要在选择中同时使用两个类,则应使用span.tlid-t.t而不是span.tlid-t t

Elements ele = doc.select("span.tlid-t.t");
String textass = ele.text();
System.out.println(textass);

将打印This is a test text

但这将选择外部跨度!如果html更改,则textass的内容也会更改。如果只想选择内部跨度的文本,则应使用span.tlid-t.t span

Elements ele = doc.select("span.tlid-t.t span");
String textass = ele.text();
System.out.println(textass);

这还将打印This is a test text