Use the following examples of how to convert HTML tables into JSON, CSV's and Excel spreadsheets using GrabzIt's Perl API. However before you start remember that after calling the URLToTable, HTMLToTable or FileToTable methods the Save or SaveTo method must be retrieve the table capture. 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.
The code example found below automatically converts the first HTML table discovered 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. However the the second table in a web page could be converted by passing a 2 to the tableNumberToInclude
method.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(2); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(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");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->tableNumberToInclude(2); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
You can also specify the targetElement
method that will ensure only tables within the specified element id will be converted.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("stocks_table"); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("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");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->targetElement("stocks_table"); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.csv");
Alternatively you can capture all tables on a web page by passing true to the includeAllTables
method, however this will only work with the XLSX format. This option will put each table in a new sheet within the generated spreadsheet workbook.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $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");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format('xlsx'); $options->includeAllTables(1); $grabzIt->FileToTable("tables.html", $options); # Then call the Save or SaveTo method $grabzIt->SaveTo("result.xlsx");
With Perl and GrabzIt you can convert HTML tables found into JSON. To get started specify the json
in the format parameter. In the example we are making a synchronous call by using the SaveTo
method, which is returning the JSON string, this could then be parsed by a library like JSON::Parse.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $grabzIt->URLToTable("https://www.tesla.com", $options); $json = $grabzIt->SaveTo();
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $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); $json = $grabzIt->SaveTo();
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->format("json"); $options->tableNumberToInclude(1); $grabzIt->FileToTable("tables.html", $options); $json = $grabzIt->SaveTo();
You can pass a custom identifier to the table methods as shown below, this value is then returned to your GrabzIt Perl handler. For instance this custom identifier could be a database identifier, allowing a screenshot to be associated with a particular database record.
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->URLToTable("https://www.tesla.com", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->HTMLToTable("<html><body><h1>Hello World!</h1></body></html>", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");
$grabzIt = GrabzItClient->new("Sign in to view your Application Key", "Sign in to view your Application Secret"); $options = GrabzItTableOptions->new(); $options->customId("123456"); $grabzIt->FileToTable("example.html", $options); # Then call the Save method $grabzIt->Save("http://www.example.com/handler.pl");