Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Div标记获取文本_Java_Jsoup_Html Parsing - Fatal编程技术网

Java 从Div标记获取文本

Java 从Div标记获取文本,java,jsoup,html-parsing,Java,Jsoup,Html Parsing,我有一个主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&g

我有一个主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/名称,不确定此样式是否相同或更改。如何使用jSoup进行提取?

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

$(文档).ready(函数(){
警报($(“.logFor div:nth child(3)”).html();
});
这是一个怪物
黑衣怪物
海因库尔特
就这么加入了!
使用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>

$(文档).ready(函数(){
警报($(“.logFor div:nth child(3)”).html();
});
这是一个怪物
黑衣怪物
海因库尔特
就这么加入了!

您可以通过以下代码实现:

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

另请查看此链接以了解有关选择器的详细信息,您可以使用以下代码实现此功能:

Document doc = Jsoup.parse(new File("test.html"), "utf-8");
Elements select = doc.select("div > div:eq(1)");
System.out.println(select.text());
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()));
    }
}
另请查看此文件以了解有关选择器的详细信息

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

这是一个怪物
黑衣怪物
这是一个怪物
黑衣怪物
真的
产生:

<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

这是一个怪物
黑衣怪物
这是一个怪物
黑衣怪物
真的

@hainKurt…我不是在前端做这件事。我不能在这里使用javascripting。我在java类文件中使用jsoup解析器来执行此操作。@hainKurt…我在前端没有执行此操作。我不能在这里使用javascripting。我在java类文件中这样做,并使用jsoup解析器。