i want to parse a html table with jsoup.
part of the html page i want to parse:
<tr>
<td class="dkHeading">A1</td>
<td class="dkHeading">A2</td>
<td class="dkHeading">A3</td>
<td class="dkHeading">A4</td>
<td class="dkHeading">A5</td>
<td class="dkHeading">A6</td>
<td class="dkHeading">A7</td>
</tr>
<tr id="RContents">
<td class="dkTextCenter">B1</td>
<td class="dkTextCenter">B2</td>
<td class="dkTextCenter">B3</td>
<td class="dkTextLeft">B4</td>
<td class="dkTextCenter">B5</td>
<td class="dkTextCenter">B6</td>
<td class="dkTextCenter">B7</td>
</tr>
<tr>
<td class="dkTextCenter">C1</td>
<td class="dkTextCenter">C2</td>
<td class="dkTextCenter">C3</td>
<td class="dkTextLeft">C4</td>
<td class="dkTextCenter">C5</td>
<td class="dkTextCenter">C6</td>
<td class="dkTextCenter">C7</td>
</tr>
<tr>
<td class="dkTextCenter">D1</td>
<td class="dkTextCenter">D2</td>
<td class="dkTextCenter">D3</td>
<td class="dkTextLeft">D4</td>
<td class="dkTextCenter">D5</td>
<td class="dkTextCenter">D6</td>
<td class="dkTextCenter">D7</td>
</tr>
how can i select all "tr" elements after (and including) that tr with id "RContents"? i tried doc.select("tr[id=RContents] > tr");
but that did't work.
您可以使用下一个兄弟姐妹选择器~:
doc.select("tr[id=RContents] ~ tr");
you can select tr Elements
, then loop
through them. since the elements are in order you can try something like this:
Document document = Jsoup.parse("YOURHTML");
Elements elements = document.select("tr");
boolean start=false;
for(Element e : elements){
if(e.hasAttr("id") && e.attr("id").equals("RContents"))){
start=true;
}
if(start){
//all tr elements including id=RContents and after
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.