Tools to Capture and Convert the Web

Capture HTML Tables from Websites with Java

Java API

You can use GrabzIt's Java API to convert HTML tables into JSON, CSV's, and Excel spreadsheets. However before you start remember that after calling the URLToTable, HTMLToTable or FileToTable methods. You have to call either the Save or SaveTo method to capture the table.

Learn if this service is suitable for you by trying the demo. The demo will show you how to capture HTML tables from a URL. This will help you quickly determine if the service meets your needs.

Basic Options

This code snippet will convert the first HTML table found in a specified webpage into a CSV document.

grabzIt.URLToTable("https://www.tesla.com");
//Then call the Save or SaveTo method
grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>");
//Then call the Save or SaveTo method
grabzIt.FileToTable("tables.html");
//Then call the Save or SaveTo method

By default this will convert the first table it identifies into a table. To convert the second table on a web page. Pass a 2 to the setTableNumberToInclude method of the TableOptions class.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTableNumberToInclude(2);

grabzIt.URLToTable("https://www.tesla.com", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTableNumberToInclude(2);

grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTableNumberToInclude(2);

grabzIt.FileToTable("tables.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");

You can also use the setTargetElement method to ensure that it only converts tables within the specified element id.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTargetElement("stocks_table");

grabzIt.URLToTable("https://www.tesla.com", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTargetElement("stocks_table");

grabzIt.HTMLToTable("<html><body><table id='stocks_table'><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setTargetElement("stocks_table");

grabzIt.FileToTable("tables.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.csv");

Or you can capture all tables on a web page by passing true to the setIncludeAllTables method. However this will only work with the XLSX and JSON formats. This option will put each table in a new sheet within the generated spreadsheet workbook.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setFormat(TableFormat.XLSX);
options.setIncludeAllTables(true);

grabzIt.URLToTable("https://www.tesla.com", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.xlsx");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setFormat(TableFormat.XLSX);
options.setIncludeAllTables(true);

grabzIt.HTMLToTable("<html><body><table><tr><th>Name</th><th>Age</th></tr>
    <tr><td>Tom</td><td>23</td></tr><tr><td>Nicola</td><td>26</td></tr>
    </table></body></html>", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.xlsx");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setFormat(TableFormat.XLSX);
options.setIncludeAllTables(true);

grabzIt.FileToTable("tables.html", options);
//Then call the Save or SaveTo method
grabzIt.SaveTo("result.xlsx");

Convert HTML Tables to JSON

GrabzIt can also convert HTML tables found on the web to JSON, just specify the JSON format instead. In the example below, the SaveTo method reads the data synchronously. Then return the result as a GrabzItFile object. However, its generally recommend doing this asynchronously.

After completing the conversion, the toString method calls to retrieve the JSON as a string. this can be then parsed by a library like google gson.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setFormat(TableFormat.JSON);
options.setTableNumberToInclude(1);

grabzIt.URLToTable("https://www.tesla.com", options);

GrabzItFile file = grabzIt.SaveTo();
if (file != null)
{
    String json = file.toString();
}

Custom Identifier

You can pass a custom identifier to the table methods as shown below. Your GrabzIt Java handler then returns this value. For example, this special name could be a database ID, connecting a screenshot to a specific database entry.

GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setCustomId("123456");

grabzIt.URLToTable("https://www.tesla.com", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setCustomId("123456");

grabzIt.HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/handler");
GrabzItClient grabzIt = new GrabzItClient("Sign in to view your Application Key", "Sign in to view your Application Secret");

TableOptions options = new TableOptions();
options.setCustomId("123456");

grabzIt.FileToTable("example.html", options);
//Then call the Save method
grabzIt.Save("http://www.example.com/handler");