[英]JSOUP: Get text after div with specific text inside
簡而言之,我正在為OSX創建古希臘協議程序,因此我需要從詞典中收集定義。
在http://biblehub.com/greek/1.htm頁面上,我需要檢索“ Strong's Exhaustive Concordance”下的文本。 問題在於HTML文件中的div與其他div包含相同的類,這使得以編程方式查找該特定div變得困難。
在JSOUP中,我在div之后搜索包含“ Strong's Exhaustive Concordance”的文本,但輸出為“ Strong's Exhaustive Concordance”,而不是單詞的定義。
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Document;
public class Greek {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://biblehub.com/greek/1.htm").get();
Elements n = doc.select("div.vheading2:containsOwn(Strong's Exhaustive Concordance) + p");
System.out.println(n.text());
}
}
我已經提出了解決方案。
import java.io.IOException;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
public class Greek {
public static void main(String[] args) throws IOException {
Document doc = Jsoup.connect("http://biblehub.com/greek/1.htm").get();
// contains an array of all elements with out desired ID
Elements n = doc.select("div.vheading2");
// cycle through the array until we find the member that contains the text above the word's definition
for (Element e : n) {
if (e.text().equalsIgnoreCase("Strong's Exhaustive Concordance")) {
// finally, we print the next element, which is our definition
System.out.println(e.nextElementSibling().text());
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.