简体   繁体   English

使用Jsoup从HTML获取多个表

[英]Getting multiple tables from HTML using Jsoup

I am trying to scrape data from multiple tables on this website: http://www.national-autograss.co.uk/march.htm 我正在尝试从此网站上的多个表格中抓取数据: http : //www.national-autograss.co.uk/march.htm

I need to keep the table data together with their respective dates located in h2 so I would like a way to do the following: 我需要将表数据及其各自的日期保存在h2中,因此我想采用以下方法:

  • Find first date header h2 查找首个日期标头h2
  • Extract table data beneath h2 (can be multiple tables) 提取h2下的表数据(可以是多个表)
  • Move on to next header and extract tables etc 移至下一个标题并提取表等

I have written code to extract all parts separately but I do not know how to extract the data so that it stays with the relevant date header. 我已经编写了分别提取所有部分的代码,但是我不知道如何提取数据,以使其与相关的日期标头保持一致。

Any help or guidance would be much appreciated. 任何帮助或指导将不胜感激。 The code I am starting with is below but like I said all it is doing is iterating through the data. 我开始的代码在下面,但是就像我说的,它所做的只是遍历数据。

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;


public class Main {

public static void main(String[] args) {

        Document doc = null;
        try {
            doc = Jsoup.connect("http://www.national-autograss.co.uk/march.htm").get();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Elements elementsTable1 = doc.select("#table1"); 
        Elements elementsTable2 = doc.select("#table2");
        Elements dateElements = doc.select("h2");

        for (int i = 0; i < dateElements.size(); i++) {
            System.out.println(dateElements.get(i).text());
            System.out.println(elementsTable1.get(i).text());
            System.out.println(elementsTable2.get(i).text());

        }

}
}

It seems that the values that you want are stored inside <tr> 's in a table where in every table the first child is a <h2> . 似乎所需的值存储在表的<tr>内,在每个表中,第一个子代是<h2>

<table align="center"><col width="200"><col width="150"><col width="100"><col width="120"><col width="330"><col width="300">
        <h2>Sunday 30 March</h2>
        <tr id="table1">
            <td><b>Club</b></td>
            <td><b>Venue</b></td>
            <td><b>Start Time</b></td>
            <td><b>Meeting Type</b></td>
            <td><b>Number of Days for Meeting</b></td>
            <td><b>Notes</b></td>

        </tr>
        <tr id="table2">
            <td>Evesham</td>
            <td>Dodwell</td>
            <td>11:00am</td>
            <td>RO</td>
            <td>Single Days Racing</td>
            <td></td>
        </tr>
</table>

My suggestion is that you search for all tables, when first child is a h2 you do something with the rest of its children: 我的建议是您搜索所有表,当第一个孩子是h2您将对其其余的孩子做一些事情:

Elements tables = doc.select("table");
for(Element table : tables) {
    if(table.child(0).tagName().equals("h2")) {
        Elements children = table.children()
    }
}

Hope this helps! 希望这可以帮助!

EDIT : You want to remove all <col> before the <h2> as they will appear before it (did not notice this before): 编辑 :您想删除所有<col><h2> <col>之前,因为它们将出现在它之前(之前没有注意到这一点):

for(Element element : doc.select("col"))
{
    element.remove();
}

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

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