Kotlin:计算<tr> Jsoup中的元素

时间:2019-11-12 16:55:12

标签: android kotlin jsoup

我在HTML中有此表格:

         <table class="table">
         <tr>
            <td class="alignLeft">XXX</td>
            <td align="right">XXX</td>
            <td align="right">XXX</td>
         </tr>
         <tr>
            <td class="alignLeft">XXX</td>
            <td align="right">XXX</td>
            <td align="right">XXX</td>
         </tr>
         <tr>
            <td colspan='2' align="right">UNTIL HERE</td>
            <td align="right" >XXX</td>
         </tr>
<td colspan='3' align='center' valign='middle'>
    <table width='90%' border='0' cellspacing="0" cellpadding="0">
        <form name='something'>
        <tr>
        <td align='center'>
            <select  name="somethingElse" size="1" class="select" onChange="document.getElementById('script').innerHTML=this.value;">
                <option label="" value="000" SELECTED></option>
                <option value="2019">2019</option>

仅当 tr 具有三个 td 时,我才需要获取 td 值 这就是我在科特林所拥有的:

 doc = connect("https://www.somepage.com").get()
        val table = doc.select("table.table")[1]
        val rows = table.select("tr")
        val tds = rows.select("td")
        for (td in tds) {
             println(td.text())
        }

我什么时候能打破呢?

<td colspan='2' align="right">UNTIL HERE</td>

2 个答案:

答案 0 :(得分:1)

还没有使用过jsoup库,但是据我了解,您需要的是:

doc = connect("https://www.somepage.com").get()
val table = doc.select("table.table")[1]
// this gives you all the rows, so you iterate over them
val rows = table.select("tr")
for (row : rows) {
    val cols = row.select("td")
    if (cols.length === 3) {
        continue;
    }
    // here the row doesn't have 3 columns
}

答案 1 :(得分:0)

val source = "<td colspan='2' align="right">UNTIL HERE</td>"
val text = source.substring(
     source.indexOf(">"),
     source.indexOf("</")
)