简体   繁体   English

Jsoup仅提取此标签

[英]Jsoup Extract only this tag

I have this html code: code html 我有这个HTML代码: 代码html

I just want "a" tag contained in "td" tag. 我只希望“ td”标签中包含“ a”标签。

I tried this: 我尝试了这个:

    Elements tables = body.getElementsByTag("table");

    for (Element table : tables) {

        if (table.className().toLowerCase().contains("infobox")) {

            Elements link=table.getElementsByTag("a");

            for(Element el:link)
                System.out.println(el.text());


            break;
        }

}

but it extracts the tag "a" of "th" and "td" while I just want the tag "a" of "td". 但是它只提取“ th”和“ td”的标签“ a”,而我只想要“ td”的标签“ a”。 Thank you. 谢谢。

EDIT I solved this way: 编辑我解决了这种方式:

Elements tables = body.getElementsByTag("table");

    for (Element table : tables) {

        if (table.className().toLowerCase().contains("infobox")) {


            Elements t1 = table.select("td");
            Elements t2 = t1.select("a");

            for(Element el:t2)
                System.out.println(el.text());


            break;
        }

}

Or simple you can do 或者简单的你可以做

for(Element e : doc.select("table.infobox td a")){
    System.out.println(e.text());
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM