首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何用jsoup动态抓取每个表tr的第一个字段?

如何用jsoup动态抓取每个表tr的第一个字段?
EN

Stack Overflow用户
提问于 2013-02-19 21:22:18
回答 1查看 77关注 0票数 0

我希望将此表的名称和计划分开存储在数据库中,以验证字段中是否已包含一些数据。

代码语言:javascript
复制
<table class="linha-horas">
 <tbody>
  <tr>
   <th><h5>Profipo p/ Itaum</h5></th>
   <th><h5>Itaum p/ Ulysses G</h5></th>
   <th><h5>Ulysses G. p/ Itaum</h5></th>
   <th><h5>Itaum p/ Profipo</h5></th>
  </tr>
  <tr>
   <td>12:49</td>
   <td>05:46</td>
   <td>05:55</td>
   <td>06:08</td>
  </tr>
  <tr>
   <td>18:05</td>
   <td>13:12</td>
   <td>06:31</td>
   <td>06:44</td>
  </tr>
  <tr>
   <td class="empty"></td>
   <td>18:29</td>
   <td>11:01</td>
   <td>11:14</td>
  </tr>
 </tbody>
</table>

下面是我想要的结果示例:

代码语言:javascript
复制
Profipo p/ Itaum
12:49
18:05

Itaum p/ Ulysses G
05:46
13:12
18:29

如果表中包含更多的计划...

Tks

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-04-01 06:40:14

提示是在select中使用:eq(),代码如下:

代码语言:javascript
复制
public final static String stack = "<table class=\"linha-horas\">"
 +"<tbody><tr><th><h5>Profipo p/ Itaum</h5></th><th><h5>Itaum p/ Ulysses G</h5>"
 + "</th><th><h5>Ulysses G. p/ Itaum</h5></th><th><h5>Itaum p/ Profipo</h5></th></tr>"
 + "<tr><td>12:49</td><td>05:46</td><td>05:55</td><td>06:08</td></tr>"
 + "<tr><td>18:05</td><td>13:12</td><td>06:31</td><td>06:44</td></tr>"
 + "<tr><td class=\"empty\"></td><td>18:29</td><td>11:01</td><td>11:14</td></tr>"
 + "</tbody></table>";

public static void main(String[] args){
    System.out.println("the html content to parse is: "+stack);
    Document doc = Jsoup.parse(stack);
    Elements ths = doc.select("th");
    for(int i = 0; i < ths.size(); i++)
    {
        System.out.println(ths.get(i).text());//header
        Elements tds = doc.select("tr td:eq("+i+")");//rows
        for(Element td : tds)
        {
            System.out.println(td.text());
        }
    }
}

下面是输出:

代码语言:javascript
复制
the html content to parse is: <table class="linha-horas"><tbody><tr><th><h5>Profipo p/ Itaum</h5></th><th><h5>Itaum p/ Ulysses G</h5></th><th><h5>Ulysses G. p/ Itaum</h5></th><th><h5>Itaum p/ Profipo</h5></th></tr><tr><td>12:49</td><td>05:46</td><td>05:55</td><td>06:08</td></tr><tr><td>18:05</td><td>13:12</td><td>06:31</td><td>06:44</td></tr><tr><td class="empty"></td><td>18:29</td><td>11:01</td><td>11:14</td></tr></tbody></table>
Profipo p/ Itaum
12:49
18:05

Itaum p/ Ulysses G
05:46
13:12
18:29
Ulysses G. p/ Itaum
05:55
06:31
11:01
Itaum p/ Profipo
06:08
06:44
11:14 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14958604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档