簡體   English   中英

JSOUP:在div之后獲取文本,其中包含特定文本

[英]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());
    }
}

您是否知道有一個非常方便的工具可以幫助您在Chrome開發工具中定位元素?

右鍵單擊要定位的元素,然后右鍵單擊->檢查,這將向您顯示該元素的HTML代碼。 右鍵單擊該元素,然后選擇復制->您將看到一系列選項,例如CSS選擇器,可供您使用的XPath :)參見以下屏幕截圖:

因此,在您的情況下,它將是: Jsoup.select("#leftbox > div > p:nth-child(74)");

在此處輸入圖片說明

我已經提出了解決方案。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM