[英]jsoup: parse data of certain tag which is just after a particular tag
我試圖通過Java中的jsoup從過去3天解析某些信息-_-,這是我的代碼:
Document document = Jsoup.connect(urlofpage).get();
Elements links = document.select(".contentBox");
for (Element link : links) {
// String name = link.text();
String title = link.select("h2").text();
String content = link.select("p").text();
System.out.println(title);
System.out.println(content);
}
它是按照指示獲取數據,取出h2和p的數據分開,但問題是,我想解析<p>
標簽內的數據,這是在每個<h2>
標簽之后。
例如(HTML內容):
<h2>main content</h2>
<div class="acx"><div>
<p>content</p>
<p>content 2</p>
<h2>content 2</h2>
<div class="acx"><div>
<p>new content od 2</p>
<p>new 2</p>
現在它應該像(在數組中)獲取:
array[0] = "content content 2",
array[1] = "new content od 2 new 2",
有解決方案嗎
您可以使用“〜”下一個元素選擇器。 例如
link.select("h2 ~ p").get(0).text(); // returns "content"
link.select("h2 ~ p").get(1).text(); // returns "new content od 2"
只需使用您的初始方法迭代所選.contentBox
類中的所有必要標記:
Document document = Jsoup.connect(urlofpage).get();
Elements links = document.select(".contentBox");
for (Element link : links) {
for (Element h2Tag : link.select("h2"))
{
System.out.println(h2Tag.text());
}
for (Element pTag : link.select("p"))
{
System.out.println(pTag.text());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.