我有一个带有多个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提取?
答案 0 :(得分:3)
您可以使用以下代码实现此目的:
Document doc = Jsoup.parse(new File("test.html"), "utf-8");
Elements select = doc.select("div > div:eq(1)");
System.out.println(select.text());
有关选择器
的详细信息,请查看此javadoc答案 1 :(得分:1)
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
答案 2 :(得分:0)
使用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>