[英]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.