简体   繁体   English

如何使用Jsoup从跨度获取内容

[英]How to get content from span with Jsoup

I am using Jsoup HTML parser to extract content from a HTML page. 我正在使用Jsoup HTML解析器从HTML页面提取内容。

<span class="mainPrice reduced_">
<span class="oPrice" data-test="preisArtikel">
<span itemprop="price" content="68.00"><span class="oPriceLeft">68</span><span             class="oPriceSeparator">,</span><span class="oPriceRight">00</span></span><span      class="oPriceSymbol oPriceSymbolRight">&euro;</span>

I want to extract the content (68.00) and I tried following: 我想提取内容(68.00),然后尝试了以下操作:

Elements price = doc.select("span.oPrice");
String priceString = price.text();

That doesn't work because the class "oPrice" occurs 44 times in the page and the string "priceString" contains 44 different prices. 这是行不通的,因为类“ oPrice”在页面中出现了44次,而字符串“ priceString”包含44个不同的价格。

Thank you for your help. 谢谢您的帮助。

Try this: 尝试这个:

     //For one element
   Element elements = document.select("span[content]").first();
        System.out.println(elements.attr("content"));

If you have multiple like same span 如果您有多个相同跨度

   //For multiple
  Elements elements = document.select("span[content]");
    for (Element element:elements){
        System.out.println(element.attr("content"));
    }

Output: 68.00 输出:68.00

On top of that Check JsoupSelector for the reference. 在该“检查JsoupSelector”上获取参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM