繁体   English   中英

使用JSOUP从html表中提取非结构化数据

[英]Extracting Unstructured data from html table using JSOUP

我一直在尝试通过jSoup使用此代码。 这个想法是从此页面中提取电影时间表:

http://www.blitzmegaplex.com/en/schedule_movie.php?id=MOV1970

到目前为止,我只能单独提取电影院的名称。 由于它被标记有特定的类名(“ separator2”)。 其余的称为“分隔符”。

我正在尝试使用for循环建立以下步骤:对于TABLE中的每个ROW:

  1. 获取电影标题
  2. 跳过它下面的一行(从步骤1行开始)。
  3. 使用名为“ separator”的类获取第二个
  4. 从其下面的所有位置获取第二个(从步骤3行开始)。 直到到达包含名为“ separator2”的类的下一行
  5. 重复该过程,直到处理完所有行。

谁能建议我该如何进行呢? 还是更好的建议?

谢谢。

到目前为止,我的代码:

public void getMovieSchedule(String movieUrl) throws IOException
{


    //URL url = new URL(movieUrl);
    //Document doc = Jsoup.parse(url, 3000);

    //Element table = doc.select("table[div=scheduletbl]").first();
    //Iterator<Element> ite = table.select("tr").iterator();
    //ite.next(); // Skip the first row.

    // Actual content
    //print(ite.next().text());

    *** CODE ABOVE DOES NOT WORK ***

    //final String urlSchedule = "http://www.blitzmegaplex.com/en/schedule_movie.php?id=MOV1970";

    Document doc = Jsoup.connect(movieUrl).get();
    Elements div = doc.select("div.panelbox");

    for(Element child : div)
    {
        Elements table = child.select("table");
        Elements row = table.select("tr"); // The actual content.

        for (Element a: row)
        {
            Elements cinemaName = a.select("td.separator2");
            print(cinemaName.text().toString());
        }
    }
}

要提取的HTML(省略了一些代码):

<table width="95%" border="0" cellpadding="2" cellspacing="0" id="scheduletbl">
    <tbody>

    <tr>
    <td colspan="3" class="separator2"><strong>BLITZMEGAPLEX - PARIS VAN JAVA, BANDUNG</strong></td>
    </tr>

    <tr>
    <td colspan="3"><img src="../img/ico_rss_schedule_white.gif" width="16" height="16" hspace="5" align="left"><strong><a href="../rss/schedule.php" class="navlink">RSS- Paris van Java</a></strong></td>
    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td colspan="2" class="separator">TUESDAY, 05 NOVEMBER 2013</td>
    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    10:30&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0100&amp;movie=MOV1970&amp;showtime=10:30&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    13:15&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0100&amp;movie=MOV1970&amp;showtime=13:15&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    16:00&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0100&amp;movie=MOV1970&amp;showtime=16:00&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    18:45&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0100&amp;movie=MOV1970&amp;showtime=18:45&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    21:30&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0100&amp;movie=MOV1970&amp;showtime=21:30&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td colspan="3" class="separator2"><strong>BLITZMEGAPLEX - GRAND INDONESIA, JAKARTA</strong></td>
    </tr>

    <tr>
    <td colspan="3"><img src="../img/ico_rss_schedule_white.gif" width="16" height="16" hspace="5" align="left"><strong><a href="../rss/schedule.php" class="navlink">RSS- Grand Indonesia</a></strong></td>
    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td colspan="2" class="separator">TUESDAY, 05 NOVEMBER 2013</td>
    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    10:45&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0200&amp;movie=MOV1970&amp;showtime=10:45&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    13:30&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0200&amp;movie=MOV1970&amp;showtime=13:30&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    16:15&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0200&amp;movie=MOV1970&amp;showtime=16:15&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    19:00&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0200&amp;movie=MOV1970&amp;showtime=19:00&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>

    </tr>
    <tr>
    <td class="separator">&nbsp;</td>
    <td width="20%" class="separator" rel="2D">
    21:45&nbsp;&nbsp;&nbsp;
    </td>
    <td width="30%" class="separator">
    <a href="https://www.blitzmegaplex.com/olb/seats.php?showdate=2013-11-05&amp;cinema=0200&amp;movie=MOV1970&amp;showtime=21:45&amp;suite=N&amp;movieformat=2D" class="navlink" target="_blank">Buy Tickets</a></td>
    </tr>
    ... MORE <tr> here ...
    </tbody></table>

如果我正确理解了您的问题,则只想从表中提取一些详细信息(即电影院名称,日期和时间),但是您会遇到麻烦,因为大多数行具有相同的className。

因此,基于此,这是我的解决方案:

Elements e = doc.select("table#scheuletbl > tbody > tr > td");
for (Element el : e) {
    if (el.hasClass("separator2")) System.out.println(el.text()); // cinema name
    else if (el.toString().contains("colspan=\"2\"")) System.out.println(el.text()); // date
    else if (el.hasAttr("rel")) System.out.println(el.text()); // times
}

将打印出:

BLITZMEGAPLEX - PARIS VAN JAVA, BANDUNG
TUESDAY, 05 NOVEMBER 2013
10:30   
13:15   
16:00   
18:45   
21:30   
BLITZMEGAPLEX - GRAND INDONESIA, JAKARTA
TUESDAY, 05 NOVEMBER 2013
10:45   
13:30   
16:15   
19:00   
21:45 

当然,此解决方案与该网站上的特定表高度耦合,但是只要该格式不经常更改且在该网站上保持一致,它就可以工作。 您可能考虑创建一个类来存储所有这些信息。

暂无
暂无

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

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