Java jsoup-如何选择特定h3(也有id和span类)后面段落中的所有链接
我试图让所有的链接更加精确,只要链接的文本在标题关系下,就可以实例化Java jsoup-如何选择特定h3(也有id和span类)后面段落中的所有链接,java,jsoup,wiki,Java,Jsoup,Wiki,我试图让所有的链接更加精确,只要链接的文本在标题关系下,就可以实例化 <h2><span class="mw-headline" id="Relations">Relations</span></h2> <h3><span class="mw-headline" id="Can_Instantiate">Can Instantiate</span></h3> <p><a href="
<h2><span class="mw-headline" id="Relations">Relations</span></h2>
<h3><span class="mw-headline" id="Can_Instantiate">Can Instantiate</span></h3>
<p><a href="/index.php/Attention_Demanding_Gameplay" title="Attention Demanding Gameplay">Attention Demanding Gameplay</a>,
<a href="/index.php?title=Conflicts&action=edit&redlink=1" class="new" title="Conflicts (page does not exist)">Conflicts</a>,
<a href="/index.php/Continuous_Goals" title="Continuous Goals">Continuous Goals</a>,
<a href="/index.php?title=Ownership&action=edit&redlink=1" class="new" title="Ownership (page does not exist)">Ownership</a>,
<a href="/index.php/Preventing_Goals" title="Preventing Goals">Preventing Goals</a>,
<a href="/index.php/Reconnaissance" title="Reconnaissance">Reconnaissance</a>,
<a href="/index.php/Stimulated_Planning" title="Stimulated Planning">Stimulated Planning</a>,
<a href="/index.php/Trade-Offs" title="Trade-Offs">Trade-Offs</a>
</p>
但两者都不起作用,我在这里有点不知所措。有人能帮我吗?您选择h3,而h3没有任何a标签。我认为您必须选择p标签Jsoup下一个同级API将帮助您。您需要选择
<h2><span class="mw-headline" id="Relations">Relations</span></h2>
这将迭代到下一个兄弟
你可以在上阅读,试试这个,这对我有用
代码:
输出:
Attention Demanding Gameplay, Conflicts, Continuous Goals, Ownership, Preventing Goals, Reconnaissance, Stimulated Planning, Trade-Offs
Attention Demanding Gameplay
Conflicts
Continuous Goals
Ownership
Preventing Goals
Reconnaissance
Stimulated Planning
Trade-Offs
使用此CSS选择器:
#mw-content-text > p:nth-child(18) > a
下面是获取标题文本的代码片段
import java.io.IOException;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Test {
public static void main(String[] args) {
try {
Document doc = Jsoup.connect("http://virt10.itu.chalmers.se/index.php/Guard").get();
List<String> titles = doc.select("#mw-content-text > p:nth-child(18) > a").eachText();
for (String title : titles) {
System.out.println(title);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
单击以快速查看。但是如果我选择p标记,我如何确保在标题关系下仅选择p?选择p的父级我认为这很容易,然后选择第一个子级,在这种情况下,你会发现代码获取p的父级的想法很简单?因此我的选择器将是元素contentinstantiate=doc.selectspan.mw-headlineRelations,h2;但是为了得到下一个兄弟,我必须使用nextElementsibling或?这将为您提供一个H2元素数组。现在,对于每个元素,您可以调用nextsibling,它将为您提供H3元素。下一个兄弟姐妹会再次给你标签。非常感谢是否不可能只获取id为Can_实例化的span class mw headline?我的亲戚们似乎没那么做。是的,你可以做到。按Id获取Can_实例化的元素。使用.parent方法获取它的父对象,然后选择nextElementSibling,它将为您提供标记。
public Node nextSibling()
final String url = "http://virt10.itu.chalmers.se/index.php/Guard";
Document doc = Jsoup.connect(url).get();
Element contentinstantiate = doc.getElementById("Can_Instantiate").parent().nextElementSibling();
for(Element e : contentinstantiate.getAllElements()){
System.out.println(e.text());
}
Attention Demanding Gameplay, Conflicts, Continuous Goals, Ownership, Preventing Goals, Reconnaissance, Stimulated Planning, Trade-Offs
Attention Demanding Gameplay
Conflicts
Continuous Goals
Ownership
Preventing Goals
Reconnaissance
Stimulated Planning
Trade-Offs
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
public class StackOverflow50897294 {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://virt10.itu.chalmers.se/index.php/Guard").get();
Elements links = doc.selectFirst("h3 > span[id=\"Can_Instantiate\"]").parent().nextElementSibling().select("a");
for (Element link : links) {
System.out.println(link.text());
}
}
}
#mw-content-text > p:nth-child(18) > a
import java.io.IOException;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
public class Test {
public static void main(String[] args) {
try {
Document doc = Jsoup.connect("http://virt10.itu.chalmers.se/index.php/Guard").get();
List<String> titles = doc.select("#mw-content-text > p:nth-child(18) > a").eachText();
for (String title : titles) {
System.out.println(title);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}