【问题标题】:Getting text from Div tags从 Div 标签中获取文本
【发布时间】:2012-03-02 08:37:42
【问题描述】:

我有一个带有多个 div 标签的主 div 标签,如下所示。子 div 标签没有区别于其他子 div 标签的类/id。现在我想从第二个子 Div 标签中提取文本值。我该怎么做?

<div class="logFor" style="position: relative; height: 101px; padding: 5px;">
     <div style="color: #6b6b6b; font-weight: bold;">This is a monster</div>
     <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">Monster in Black</div>
     <div style="position: absolute; left: 5px; bottom: 0;">
     <div style="position: absolute; right: 5px; bottom: 0;">
</div>

我想得到文本“黑衣怪物”。此 Div 没有 ID/名称,并且不确定此样式是否相同或更改。我将如何使用 jSoup 进行提取?

【问题讨论】:

    标签: java jsoup html-parsing


    【解决方案1】:

    您可以使用以下代码实现:

    Document doc = Jsoup.parse(new File("test.html"), "utf-8");
    Elements select = doc.select("div > div:eq(1)");
    System.out.println(select.text());
    

    还可以查看javadoc 了解有关 Selector 的详细信息

    【讨论】:

      【解决方案2】:
      package stackoverflow;
      
      import java.io.IOException;
      import java.io.InputStream;
      
      import org.apache.commons.io.IOUtils;
      import org.jsoup.Jsoup;
      import org.jsoup.nodes.Document;
      import org.jsoup.nodes.Element;
      import org.jsoup.select.Elements;
      
      public class JSoupTest {
          public static void main(String[] args) throws IOException {
              InputStream in = JSoupTest.class.getResourceAsStream("JSoupTest.txt");
      
              String html = IOUtils.toString(in);
      
              Document doc = Jsoup.parse(html);
      
              Elements divs = doc.select("DIV");
              System.out.println(divs);
      
              Element div = divs.get(2);
              System.out.println("Monster in Black".equals(div.text()));
          }
      }
      

      生产:

      <div class="logFor" style="position: relative; height: 101px; padding: 5px;"> 
       <div style="color: #6b6b6b; font-weight: bold;">
        This is a monster
       </div> 
       <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">
        Monster in Black
       </div> 
       <div style="position: absolute; left: 5px; bottom: 0;"> 
        <div style="position: absolute; right: 5px; bottom: 0;"> 
        </div> 
       </div>
      </div>
      <div style="color: #6b6b6b; font-weight: bold;">
       This is a monster
      </div>
      <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">
       Monster in Black
      </div>
      <div style="position: absolute; left: 5px; bottom: 0;"> 
       <div style="position: absolute; right: 5px; bottom: 0;"> 
       </div> 
      </div>
      <div style="position: absolute; right: 5px; bottom: 0;"> 
      </div>
      true
      

      【讨论】:

        【解决方案3】:

        使用 jquery

        <html>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript" ></script>
        <script>
        $(document).ready(function() {
            alert($(".logFor div:nth-child(3)").html());
        });
        </script>
        <body>
        <div class="logFor" style="position: relative; height: 101px; padding: 5px;">
             <div style="color: #6b6b6b; font-weight: bold;">This is a monster</div>
             <div style="overflow: hidden; height: 28px; margin-top: 3px; color: #1b1f2e;">Monster in Black</div>
             <div style="position: absolute; left: 5px; bottom: 0;">HainKurt</div>
             <div style="position: absolute; right: 5px; bottom: 0;">Just joined to SO!</div>
        </div>
        </body>
        </html>
        

        【讨论】:

        • @hainKurt...我没有在前端这样做。我不能在这里使用javascripting。我在 java 类文件中执行此操作并使用 jsoup 解析器。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        • 1970-01-01
        • 2015-10-06
        相关资源
        最近更新 更多