有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用单个类名从html标记中获取文本,html标记将包含多个类

我有一个html行,其中标签中有标签,一个标签包含多个类。我需要用单个类名提取文本(我只知道一个类名)

<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>

我只知道类名Allsmall,我想使用java中的Jsoup从html行中提取文本“ExtractThisText”


共 (1) 个答案

  1. # 1 楼答案

    您可以使用选择器语法根据特定元素的CSSclass属性检索该元素:

    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