从 Div 标签获取文本

发布于 2025-01-03 23:01:43 字数 643 浏览 1 评论 0原文

我有一个主 Div 标签,其中包含多个 div 标签,如下所示。子 Div 标签没有与其他子 div 标签区分开的 class/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/name,不确定这个样式是否相同或改变。我如何使用 jSoup 提取?

I have a main Div tag with multiple div tags as below. The child Div tags have no class/id that distinguishes from the other child div tags. Now I want to extract the text value from the 2nd child Div tag. How can i do that?

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

I want to get the text "Monster in Black". This Div doesnt have an id/name and not sure if this style would be same or change. How would i extract using jSoup?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

甜妞爱困 2025-01-10 23:01:43

您可以使用以下代码实现这一点:

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

另请查看此 javadoc 了解详细信息关于选择器

You can achieve that with following code:

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

Also check out this javadoc for details about Selector

请远离我 2025-01-10 23:01:43
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
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()));
    }
}

Produces:

<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
若沐 2025-01-10 23:01:43

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

use 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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文