Tools to Capture and Convert the Web

Capture HTML Tables from Websites with Node.js

Node.js API

There are multiple ways of converting HTML tables into JSON, CSV's and Excel spreadsheets using GrabzIt's Node.js API, detailed here are some of the most useful techniques. However before you start remember that after calling the url_to_table, html_to_table or file_to_table methods the save or save_to method must be called to capture the table. If you want to quickly see if this service is right for you, you can try a live demo of capturing HTML tables from a URL.

Basic Options

This particular method call will convert the first HTML table in the webpage of the specified URL, into a CSV document. This code snippet will convert the first HTML table found in a specified webpage or HTML input into a CSV document.

client.url_to_table("https://www.tesla.com");
//Then call the save or save_to method
client.html_to_table("<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 save_to method
client.file_to_table("tables.html");
//Then call the save or save_to method

By default this will convert the first table it identifies into a table. However the the second table in a web page could be converted by passing a 2 to the tableNumberToInclude property.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.html_to_table("<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 save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"tableNumberToInclude":2};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

You can also specify the targetElement property that will ensure only tables within the specified element id will be converted.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.html_to_table("<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 save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"targetElement":"stocks_table"};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.csv", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

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

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.url_to_table("https://www.tesla.com", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.html_to_table("<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 save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","xlsx","includeHeaderNames":true,"includeAllTables":true};

client.file_to_table("tables.html", options);
//Then call the save or save_to method
client.save_to("result.xlsx", function (error, id){
    //this callback is called once the capture is downloaded
    if (error != null){
        throw error;
    }
});

Convert HTML Tables to JSON

By using Node.js and GrabzIt you can convert HTML tables into JSON, just specify json in the format parameter. As shown in the example below once the save_to method is finished the oncomplete function is called with the JSON in the result variable this is then parsed by the inbuilt Node.js JSON.parse function to create a object that represents the HTML table.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"format","json","includeHeaderNames":true,"includeAllTables":true};
client.url_to_table("https://www.tesla.com", options);

client.save_to(null, function(error, result){
    if (result != null)
    {
        var tableObj = JSON.parse(result);
    }
});

Custom Identifier

You can pass a custom identifier to the table methods as shown below, this value is then returned to your GrabzIt Node.js handler. For instance this custom identifier could be a database identifier, allowing a screenshot to be associated with a particular database record.

var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.url_to_table("https://www.tesla.com", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.html_to_table("<html><body><h1>Hello World!</h1></body></html>", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});
var grabzit = require('grabzit');

var client = new grabzit("Sign in to view your Application Key", "Sign in to view your Application Secret");

var options = {"customId":123456};

client.file_to_table("example.html", options);
//Then call the save method
client.save("http://www.example.com/handler", function (error, id){
    if (error != null){
        throw error;
    }
});