[英]jsoup get the next div element
How can I get the first div after the h1 tag . 如何获取h1标签之后的第一个div。
The html: 的HTML:
<h1> Shalom </h1>
<b> Tov </b>
<div> ddd </div> <! I need to take this div >
My java jsoup code 我的Java Jsoup代码
Elements apresh = doc.select("h1 ~ div");
String csdsdsdf = apresh.html();
System.out.printf(csdsdsdf);
But it doesn't work. 但这是行不通的。 Can you help me ?
你能帮助我吗 ?
From what you have mentioned in the comments, I believe you are trying to extract the first element from the matching elements based on your selector "h1 ~ div"
. 根据您在评论中提到的内容,我相信您正在尝试基于选择器
"h1 ~ div"
从匹配的元素中提取第一个元素。
You can use the below provided method from the API. 您可以使用API提供的以下方法。
public Element first() : Get the first matched element. public Element first() :获取第一个匹配的元素。
I've found two ways to do this: 我发现了两种方法可以做到这一点:
Document doc = Jsoup.parse("<h1> Shalom </h1>" +
"<b> Tov </b>" +
"<div> ddd </div>");
// 1 Select DIV which is after B which is after H1.
System.out.println(doc.select("h1 + b + div"));
// 2 More flexible solution which involves going one level up to parent
// and then selecting the first DIV.
System.out.println(doc.select("h1").first().parent().select("div").first());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.