簡體   English   中英

使用jSoup解析內部html標簽

[英]Parse the inner html tags using jSoup

我想在使用Jsoup庫的網站中找到重要的鏈接。 因此,假設有以下代碼:

<h1><a href="http://example.com">This is important </a></h1>

現在,在解析的同時,我們如何發現標簽a在h1標簽內?

您可以這樣操作:

File input = new File("/tmp/input.html");
Document doc = Jsoup.parse(input, "UTF-8", "http://example.com/");

Elements headlinesCat1 = doc.getElementsByTag("h1");
for (Element headline : headlinesCat1) {
    Elements importantLinks = headline.getElementsByTag("a");
    for (Element link : importantLinks) {
        String linkHref = link.attr("href");
        String linkText = link.text();
        System.out.println(linkHref);
    }
}

摘自JSoup Cookbook

使用選擇器:

Elements elements = doc.select("h1 > a");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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