Sample code for 30+ languages & platforms
Lua Requires Chilkat v9.5.0.77+

HTML Table to CSV

See more HTML-to-XML/Text Examples

Demonstrates a method for converting an HTML table to a CSV file.

Chilkat Lua Downloads

Lua
local chilkat = require("chilkat")

-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.

-- First download the HTML containing the table
local http = chilkat.Http.new()
local bdHtml = chilkat.BinData.new()

local success = http:quick_get_bd("https://example-code.com/data/etf_table.html", bdHtml)
if not success then
    print(http.last_error_text)
    return
end

-- Convert to XML.
local htx = chilkat.HtmlToXml.new()
htx:set_html_bd(bdHtml)

local sbXml = chilkat.StringBuilder.new()
htx:to_xml_sb(sbXml)

local xml = chilkat.Xml.new()
xml:load_sb(sbXml, true)

-- Remove attributes and sub-trees we don't need.
-- (In other words, we're getting rid of clutter...)
local numRemoved = xml:prune_tag("thead")
numRemoved = xml:prune_attribute("style")
numRemoved = xml:prune_attribute("class")

-- Scrub the element and attribute content.
xml:scrub("ContentTrimEnds,ContentTrimInside,AttrTrimEnds,AttrTrimInside")

-- Let's see what we have...
print(xml:get_xml())

-- We have the following XML.
-- Copy this XML into the online tool at Generate Parsing Code from XML
-- as a starting point for accessing the data..

-- <?xml version="1.0" encoding="utf-8"?>
-- <root>
--     <html>
--         <head>
--             <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
--         </head>
--         <body text="#000000" bgcolor="#FFFFFF">
--             <div>
--                 <div>
--                     <table role="grid" data-scrollx="true" data-sortdirection="desc" data-sorton="-1"/>
--                 </div>
--             </div>
--             <div>
--                 <table id="topHoldingsTable" role="grid" data-scrollx="true" data-sortdirection="desc" data-sorton="-1">
--                     <tbody>
--                         <tr role="row">
--                             <td>
--                                 <text>ITUB4</text>
--                             </td>
--                             <td>
--                                 <text>ITAU UNIBANCO HOLDING PREF SA</text>
--                             </td>
--                             <td>
--                                 <text>Financials</text>
--                             </td>
--                             <td>
--                                 <text>Brazil</text>
--                             </td>
--                             <td>
--                                 <text>10.94</text>
--                             </td>
--                             <td>
--                                 <text>998,954,813.73</text>
--                             </td>
--                         </tr>
--                         <tr role="row">
--                             <td>
--                                 <text>BBDC4</text>
--                             </td>
--                             <td>
--                                 <text>BANCO BRADESCO PREF SA</text>
--                             </td>
--                             <td>
--                                 <text>Financials</text>
--                             </td>
--                             <td>
--                                 <text>Brazil</text>
--                             </td>
--                             <td>
--                                 <text>9.01</text>
--                             </td>
--                             <td>
--                                 <text>822,164,622.75</text>
--                             </td>
--                         </tr>
--             ...
--             ...
--             ...
--                     </tbody>
--                 </table>
--             </div>
--         </body>
--     </html>
-- </root>

-- This is the code generated by the online tool:
--
local table_role = ""
local table_data_scrollx = ""
local table_data_sortdirection = ""
local table_data_sorton = ""
local table_id = ""
local j = 0
local count_j = 0
local tr_role = ""
local k = 0
local count_k = 0
local tagPath = ""
local text = ""

local i = 0
local count_i = xml:num_children_having_tag("html|body|div")
while i < count_i do
    xml.i = i
    table_role = xml:chilkat_path("html|body|div[i]|div|table|(role)")
    table_data_scrollx = xml:chilkat_path("html|body|div[i]|div|table|(data-scrollx)")
    table_data_sortdirection = xml:chilkat_path("html|body|div[i]|div|table|(data-sortdirection)")
    table_data_sorton = xml:chilkat_path("html|body|div[i]|div|table|(data-sorton)")
    table_id = xml:chilkat_path("html|body|div[i]|table|(id)")
    table_role = xml:chilkat_path("html|body|div[i]|table|(role)")
    table_data_scrollx = xml:chilkat_path("html|body|div[i]|table|(data-scrollx)")
    table_data_sortdirection = xml:chilkat_path("html|body|div[i]|table|(data-sortdirection)")
    table_data_sorton = xml:chilkat_path("html|body|div[i]|table|(data-sorton)")
    j = 0
    count_j = xml:num_children_having_tag("html|body|div[i]|table|tbody|tr")
    while j < count_j do
        xml.j = j
        tr_role = xml:chilkat_path("html|body|div[i]|table|tbody|tr[j]|(role)")
        k = 0
        count_k = xml:num_children_having_tag("html|body|div[i]|table|tbody|tr[j]|td")
        while k < count_k do
            xml.k = k
            text = xml:get_child_content("html|body|div[i]|table|tbody|tr[j]|td[k]|text")
            k = k + 1
        end

        j = j + 1
    end

    i = i + 1
end

-- Let's modify the above code to build the CSV.
local csv = chilkat.Csv.new()
csv:set_column_name(0, "Ticker")
csv:set_column_name(1, "Name")
csv:set_column_name(2, "Sector")
csv:set_column_name(3, "Country")
csv:set_column_name(4, "Weight")
csv:set_column_name(5, "Notional Vaue")

i = 0
count_i = xml:num_children_having_tag("html|body|div")
while i < count_i do
    xml.i = i
    j = 0
    count_j = xml:num_children_having_tag("html|body|div[i]|table|tbody|tr")
    while j < count_j do
        xml.j = j
        k = 0
        count_k = xml:num_children_having_tag("html|body|div[i]|table|tbody|tr[j]|td")
        while k < count_k do
            xml.k = k
            csv:set_cell(j, k, xml:get_child_content("html|body|div[i]|table|tbody|tr[j]|td[k]|text"))
            k = k + 1
        end

        j = j + 1
    end

    i = i + 1
end

csv:save_file("qa_output/brasil_etf.csv")
local csvStr = csv:save_to_string()
print(csvStr)

-- Our CSV looks like this:
-- Ticker,Name,Sector,Country,Weight,Notional Vaue
-- ITUB4,ITAU UNIBANCO HOLDING PREF SA,Financials,Brazil,10.94,"998,954,813.73"
-- BBDC4,BANCO BRADESCO PREF SA,Financials,Brazil,9.01,"822,164,622.75"
-- VALE3,CIA VALE DO RIO DOCE SH,Materials,Brazil,8.60,"785,290,260.07"
-- PETR4,PETROLEO BRASILEIRO PREF SA,Energy,Brazil,5.68,"518,124,434.10"
-- PETR3,PETROBRAS,Energy,Brazil,4.86,"443,254,438.53"
-- B3SA3,B3 BRASIL BOLSA BALCAO SA,Financials,Brazil,4.57,"417,636,740.16"
-- ABEV3,AMBEV SA,Consumer Staples,Brazil,4.57,"417,216,913.63"
-- BBAS3,BANCO DO BRASIL SA,Financials,Brazil,3.25,"296,921,232.15"
-- ITSA4,ITAUSA INVESTIMENTOS ITAU PREF SA,Financials,Brazil,2.90,"265,153,684.52"
-- LREN3,LOJAS RENNER SA,Consumer Discretionary,Brazil,2.25,"205,832,175.98"
--