简体   繁体   中英

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

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:

  • Find first date header h2
  • Extract table data beneath h2 (can be multiple tables)
  • 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> .

<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:

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):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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