【问题标题】:jsoup extract elements underneath a specific tagjsoup 提取特定标签下的元素
【发布时间】:2013-08-19 10:28:05
【问题描述】:

我正在尝试提取特定标签下的几个元素 我有一堆<h5>,我想用它们下面的<h6><table> 提取它们。 我遇到的问题是: a) 我有几个<h5> 标签 b) <h6><table> 不是 <h5> 的孩子/兄弟姐妹。所以例如h5 > table 将不起作用。

所以我想最后得到的是: 从这个网站: http://tcat.nextinsight.com/routes.php?mrnid=453

Route 13 周一至周五,<h6> 入站和餐桌,以及 Route 13 周一至周五,<h6> 出站和餐桌。

一旦我有了整个表格,我就可以使用这个示例 How to get a table from an html page using JAVA 来处理表格

示例结构:(也可以在给定的 url 中找到)

<table width="890" border="0" cellspacing="3">
        <tr>
          <td colspan="20" bgcolor="#8cd2ef" class="heading"><h6>Outbound from center of Ithaca</h6></td>
        </tr>
        <br><h5>Route 13 - Saturday</h5><tr class="tableSub"><td>Green @ Commons</td>
<td>Seneca @ Commons</td>
<td>Third @ Hancock</td>
<td>Aldi</td>
<td>Lake @ Ithaca HS</td>
<td>Stewart Park</td>
<td>Shops at Ithaca Mall @ Sears</td>
</tr>

【问题讨论】:

标签: jsoup


【解决方案1】:

选择器:

h5:contains(Route 13 Monday - Friday) + table

像这样使用:

Elements tables = doc.select("h5:contains(Route 13 Monday - Friday) + table");

将为您获取前面有 &lt;h5&gt; 内容 "Route 13 Monday - Friday" 的每个表。

使用您提供的 URL 检查有效的 sn-p:

public static void main(String[] args) throws Exception {
    Document doc = Jsoup.connect("http://tcat.nextinsight.com/routes.php?mrnid=453").get();
    System.out.println(doc.title());
    Elements tables = doc.select("h5:contains(Route 13 Monday - Friday) + table");
    for (Element table : tables) {
        System.out.println(table);
        System.out.println("#\n#\n#\n#");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多