简体   繁体   中英

Get text from html tag using single class name, the html tag will contain multiple class

I have a html line where there are tags inside tags, a single tag my contain multiple class. I need to extract the text with single class name(i know only one class name)

<p class="Body1"><span class="style3"></span><span class="style1">W</span><span class="Allsmall style5">extract this text </span><span class="style5">unwanted text </span></p>

I know the class name Allsmall alone i want to extract the text "extract this text" from the html line using Jsoup in java.

You can use the selector syntax to retrieve a specific element based on its CSS class attribute:

Document doc = Jsoup.parse(
  new File("input.html"), 
  "UTF-8", 
  "http://sample.com/");

Element allSmallSpan = doc.select("span.Allsmall").first(); // Retrive the first <span> element which belongs to "Allsmall" class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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