【问题标题】:Get text from span using Jsoup (java)使用 Jsoup (java) 从 span 获取文本
【发布时间】:2026-02-10 19:55:02
【问题描述】:

您好,我想在“cotation”之后获取值,但我不知道该怎么做。

<span id="brs-sl57350199c5a53">
    <span class="cotation">16.33 (c)</span>
</span>

我正在使用 Jsoup,这是我的代码:

Document doc = Jsoup.connect("http://www.boursorama.com/bourse/actions/cours_az.phtml").get(); 

Element loginform = doc.getElementById("content");  //brs-sl5734ff406037d
Elements inputElements = loginform.getElementsByTag("span");  

for (Element inputElement : inputElements) {  
    String key = inputElement.attr("class");  
    System.out.println("Param name: "+ key);
}

这就是我所拥有的:

Param name: cotation

你有什么想法能够获得价值而不是“coation”吗?

提前致谢。

【问题讨论】:

    标签: java html text jsoup


    【解决方案1】:

    在你的循环中添加这个:

    if ("cotation".equals(key)) {
        System.out.println(inputElement.html());
    }
    

    你会得到:

    16.33 (c)
    

    【讨论】:

    • 谢谢,这正是我所需要的!!