GrabzIt
Tools to Capture and Convert the Web

Capture HTML Tables from Websites with RubyRuby API

Warning At least a Entry Package is required to use HTML table capture. Try it for free with a 7 day free trial. Then from $5.99 a month, unless cancelled.Start 7 Day Free Trial

Converting HTML tables into JSON, CSV's and Excel spreadsheets using GrabzIt's Ruby API is east just follow the examples shown here. 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.

Basic Options

The below example converts the first HTML table in a specified webpage into a CSV document.

grabzItClient.url_to_table("http://www.google.com")
grabzItClient.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>")
grabzItClient.file_to_table("tables.html")

If you don't want to automatically convert the first table in a webpage you can specify the tableNumberToInclude method. For instance specifiying a 2 would convert the second table found in a web page.

options = GrabzIt::TableOptions.new()
options.tableNumberToInclude(2)

grabzItClient.url_to_table("http://www.google.com", options)
options = GrabzIt::TableOptions.new()
options.tableNumberToInclude(2)

grabzItClient.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)
options = GrabzIt::TableOptions.new()
options.tableNumberToInclude(2)

grabzItClient.file_to_table("tables.html", options)

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

options = GrabzIt::TableOptions.new()
options.targetElement("stocks_table")

grabzItClient.url_to_table("http://www.google.com", options)
options = GrabzIt::TableOptions.new()
options.targetElement("stocks_table")

grabzItClient.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)
options = GrabzIt::TableOptions.new()
options.targetElement("stocks_table")

grabzItClient.file_to_table("tables.html", options)

If you use the XLSX format you can capture all the tables on a webpage by passing true to the includeAllTables method. This will then put each table in a new sheet within the spreadsheet workbook.

options = GrabzIt::TableOptions.new()
options.format("xlsx")
options.includeAllTables(true)

grabzItClient.url_to_table("http://www.google.com", options)
options = GrabzIt::TableOptions.new()
options.format("xlsx")
options.includeAllTables(true)

grabzItClient.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)
options = GrabzIt::TableOptions.new()
options.format("xlsx")
options.includeAllTables(true)

grabzItClient.file_to_table("tables.html", options)

Convert HTML Tables to JSON

With GrabzIt, Ruby can easily convert HTML tables into JSON to do this specify json in the format parameter. In the example below the data is read synchronously by using the save_to method, to get the JSON as a string. This can be then parsed by a library like json gem.

options = GrabzIt::TableOptions.new()
options.format("json")
options.tableNumberToInclude(1)

grabzItClient.url_to_table("http://www.google.com", options)

json = grabzItClient.save_to()
options = GrabzIt::TableOptions.new()
options.format("json")
options.tableNumberToInclude(1)

grabzItClient.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)

json = grabzItClient.save_to()
options = GrabzIt::TableOptions.new()
options.format("json")
options.tableNumberToInclude(1)

grabzItClient.file_to_table("tables.html", options)

json = grabzItClient.save_to()

Custom Identifier

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

options = GrabzIt::TableOptions.new()
options.customId("123456")

grabzItClient.url_to_table("http://www.google.com", options)
options = GrabzIt::TableOptions.new()
options.customId("123456")

grabzItClient.html_to_table("<html><body><h1>Hello World!</h1></body></html>", options)
options = GrabzIt::TableOptions.new()
options.customId("123456")

grabzItClient.file_to_table("example.html", options)