java - 如何从html表及其内部表中提取数据?

标签 java html jsoup

我有一个 html 表结构,其中一些数据位于主表中,一些数据位于 td 元素内的嵌套表中。

我只需要所需的 5 个数据(带有 ** xx ** 指示),以便我可以将其作为一行导出到 Excel。

<table cellpadding="2" cellspacing="0" width="100%" class="chart">
              <tr>
              <td>**Text 1**</td>         
                <td>
                  <table cellpadding="2" cellspacing="0">
                    <tr>
                      <td>some useless data</td>
                      <td>**Text 2**</td>
                    </tr>
                  </table>
                </td>
                <td>**Text 3**</td>
                <td>**Text 4**</td>
                <td>**Text 5**</td>
              </tr>
</table>

我的代码是这样的:

    for (Element row : excel.select("tr")) {
        // create row for each tag
        header = sheet.createRow(rowCount);
        // loop through all th tag
        Elements ths = row.select("th");
        int count = 0;
        for (Element element : ths) {
            // set header style
            cell = header.createCell(count);
            cell.setCellValue(element.text());
            cell.setCellStyle(headerStyle);
            count++;
        }
        // now loop through all td tag
        Elements tds = row.select("td");
        count = 0;
        for (Element element : tds) {
            if(!element.text().isEmpty()){
                cell = header.createCell(count);
                cell.setCellValue(element.text());
                count++;
                }
        }

这里的问题是输出不符合预期。

在 Excel 中看起来像这样:

  Row1:  Text 1 | Text 2 | useless data | Text 2 | Text 3 | Text 4 | Text 5 |
  Row2:  useless data | Text 2 |
<小时/>

附加信息:为了简化问题,省略了标签。

我想要的是

 Row1:  Text 1 | Text 2 | Text 3 | Text 4 | Text 5 |

最佳答案

<强>1。两行

我猜excel是文档或表格。无论如何,当你选择 excel.select("tr") 您还可以选择内部表tr。为了防止这种情况,您需要使 css 选择器更加具体。如果我假设 excel 是文档,我可以这样做

Elements outerTrs = excel.select("table.chart>tbody>tr");

在您的代码上下文中:

for (Element row : excel.select("table.chart>tbody>tr")) {

说明: 如果 tbody 元素不存在,Jsoup 将在表中创建该元素。使用选择器,我确保只选择外部表的直接子 tr 元素,我可以做到这一点,因为我知道外部表的类名,而且它看起来是唯一的。

<强>2。意外的列数

这是因为您的 select row.select("td") 语句选择了包含内部表的 td。如果您只想要没有子元素的 tds,您可以使用:

Elements tds = row.select("td");
count = 0;
for (Element element : tds) {
if(!element.text().isEmpty() && element.children().isEmpty()){
    count++;
    System.out.println("line "+count+" text = '"+element.text()+"'");
}

<强>3。无用数据

要摆脱这个问题,您只需将其过滤掉即可。从您的示例来看,不清楚何时存在无用数据。它总是在内表中的第一个td吗?如果是这样,你可以这样做(完整的解决方案)

Document excel = Jsoup.parse(tab);

for (Element row : excel.select("table.chart>tbody>tr")) {
    Elements tds = row.select("td");
    int count = 0;

    Element junkTd = row.select("td table td").first();

    for (Element element : tds) {
        if(!element.text().isEmpty() 
                && element.children().isEmpty()
                && !element.equals(junkTd)){

            count++;
            System.out.println("line "+count+" text = '"+element.text()+"'");
        }
    }
}

关于java - 如何从html表及其内部表中提取数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31761562/

相关文章:

java - Hibernate 映射已被删除,但表中的行未被删除

java - Eclipse 是如何快速显示启动界面的

javascript - 移动网络应用 : Using same set of images in multiple resolution devices

java - 使用 JSoup 解析音频 Src

java - XML 解析为 SQL 代码

java - VLCJ 无法正常工作

java - 在java上读取.dat文件获取null

html - 我如何以正确的方式创建社交图标(如图表)以提高响应能力

html - Div 背景不会显示嵌套子项

java - 解析复杂的 li 标签