简体   繁体   English

Jsoup删除特定的TR ID

[英]Jsoup remove specific TR id

I try to remove a "TR" on a table with specific id, where I'm got from 我尝试从具有特定ID的表上删除“ TR”

String url = "http://citraslider.blogspot.com/2014/03/table-model.html";
Document doc = Jsoup.connect(url).get();
System.out.println(doc);

And print: 并打印:

<table id="mt" border="0" cellpadding="2" cellspacing="0" width="100%" class="hbtbl">
    <tr id="1"><td class="aa"><div class="bb">11</div><a href="mailto:asd@yahoo.com" target="_blank"><b class="nme pn_std">bua</b></a>: ask 1 ?</td></tr>
    <tr id="2"><td class="a"><div class="b">12</div><a href="mailto:asd@yahoo.com" target="_blank"><b class="nme pn_std">bua</b></a>: ask 2 ?</td></tr>
    <tr id="3"><td class="aa"><div class="bb">13</div><a href="mailto:asd@yahoo.com" target="_blank"><b class="nme pn_std">bua</b></a>: ask 3 ?</td></tr>
    <tr id="-1"><td class="a"><div align="center"><a href="javascript:void(window.open('index.php?mid=1&btag=awe&i='+((cf.op)?cf.op:8304), 'archive', 'width=320,height=400,resizable=yes,scrollbars=yes'));">[prev]</a></div></td></tr>
</table>

Here is: 这是:

Elements elemen= doc.select("tr");
Elements el = doc.getElementsByAttributeValue("id", "-1");
el.remove(0);
System.out.println(el); // Here its work
for (Element e : elemen) {
    System.out.println(e.text()+":"+e.attr("id")); // But at this line still show [prev], tr id="-1" still show
    e.getElementsByAttributeValue("id", "-1").remove();
}

So, how i can remove "tr id=-1" on a loop results? 那么,如何在循环结果中删除“ tr id = -1”?

    <tr id="-1"><td class="a"><div align="center"><a href="javascript:void(window.open('index.php?mid=1&btag=awe&i='+((cf.op)?cf.op:8304), 'archive', 'width=320,height=400,resizable=yes,scrollbars=yes'));">[prev]</a></div></td></tr>

Just change your selector to exclude tr for id = -1 只需更改选择器以排除id = -1 tr

Elements elemen= doc.select("tr").not("tr#-1");

More information on the selector syntax here . 有关选择器语法的更多信息,请参见此处

Code becomes, 代码变成

Elements elemen= doc.select("tr").not("tr#-1");
        for (Element e : elemen) {
            System.out.println(e.text()+":"+e.attr("id"));

        }

Gives, 给人,

11bua: ask 1 ?:1
12bua: ask 2 ?:2
13bua: ask 3 ?:3

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

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