Jsoup 如何解析 span class="hps" 内的文本

Jsoup how to parse text inside span class="hps"

<span id="result_box" class="short_text" lang="es">
  <span class="hps">
    hello
  </span>
  <span class="hps">
    world
  </span>
</span>

我想使用 Jsoup 获取 hello world 字符串,但我不知道该怎么做。

使用 Jsoup.parse 获取 html 文档。 Select 您想要使用 css 选择器的元素,例如:span.hps (http://jsoup.org/apidocs/org/jsoup/select/Selector.html)

    Document doc = Jsoup.parse("<span id=\"result_box\" class=\"short_text\" lang=\"es\">\n" +
                "  <span class=\"hps\">\n" +
                "    hello\n" +
                "  </span>\n" +
                "  <span class=\"hps\">\n" +
                "    world\n" +
                "  </span>\n" +
                "</span>");

    System.out.println(doc.html());

    Elements els = doc.select("span.hps");
    for(Element e:els){
        System.out.print(e.text());
    }

如果您不关心每个元素的值,您可以替换 for 循环:

els.text()