我尝试删除具有特定ID的表上的“TR”,我来自
String url = "http://citraslider.blogspot.com/2014/03/table-model.html";
Document doc = Jsoup.connect(url).get();
System.out.println(doc);
并打印:
<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>
这是:
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();
}
那么,我如何在循环结果中删除“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>
答案 0 :(得分:0)
只需更改您的选择器即可排除tr
id = -1
Elements elemen= doc.select("tr").not("tr#-1");
有关selector syntax here的更多信息。
代码变为,
Elements elemen= doc.select("tr").not("tr#-1");
for (Element e : elemen) {
System.out.println(e.text()+":"+e.attr("id"));
}
给出,
11bua: ask 1 ?:1
12bua: ask 2 ?:2
13bua: ask 3 ?:3