Sample code for 30+ languages & platforms
AutoIt

HTML Table to CSV

See more HTML-to-XML/Text Examples

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

Note: This example requires Chilkat v9.5.0.77 or greater.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

; 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
$oHttp = ObjCreate("Chilkat.Http")
$oBdHtml = ObjCreate("Chilkat.BinData")

$bSuccess = $oHttp.QuickGetBd("https://example-code.com/data/etf_table.html",$oBdHtml)
If ($bSuccess <> True) Then
    ConsoleWrite($oHttp.LastErrorText & @CRLF)
    Exit
EndIf

; Convert to XML.
$oHtx = ObjCreate("Chilkat.HtmlToXml")
$oHtx.SetHtmlBd($oBdHtml)

$oSbXml = ObjCreate("Chilkat.StringBuilder")
$oHtx.ToXmlSb($oSbXml)

$oXml = ObjCreate("Chilkat.Xml")
$oXml.LoadSb($oSbXml,True)

; Remove attributes and sub-trees we don't need.
; (In other words, we're getting rid of clutter...)
Local $iNumRemoved = $oXml.PruneTag("thead")
$iNumRemoved = $oXml.PruneAttribute("style")
$iNumRemoved = $oXml.PruneAttribute("class")

; Scrub the element and attribute content.
$oXml.Scrub "ContentTrimEnds,ContentTrimInside,AttrTrimEnds,AttrTrimInside"

; Let's see what we have...
ConsoleWrite($oXml.GetXml() & @CRLF)

; 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 $i
Local $iCount_i
Local $sTable_role
Local $sTable_data_scrollx
Local $sTable_data_sortdirection
Local $sTable_data_sorton
Local $sTable_id
Local $iJ
Local $iCount_j
Local $sTr_role
Local $iK
Local $iCount_k
Local $sTagPath
Local $sText

$i = 0
$iCount_i = $oXml.NumChildrenHavingTag("html|body|div")
While $i < $iCount_i
    $oXml.I = $i
    $sTable_role = $oXml.ChilkatPath("html|body|div[i]|div|table|(role)")
    $sTable_data_scrollx = $oXml.ChilkatPath("html|body|div[i]|div|table|(data-scrollx)")
    $sTable_data_sortdirection = $oXml.ChilkatPath("html|body|div[i]|div|table|(data-sortdirection)")
    $sTable_data_sorton = $oXml.ChilkatPath("html|body|div[i]|div|table|(data-sorton)")
    $sTable_id = $oXml.ChilkatPath("html|body|div[i]|table|(id)")
    $sTable_role = $oXml.ChilkatPath("html|body|div[i]|table|(role)")
    $sTable_data_scrollx = $oXml.ChilkatPath("html|body|div[i]|table|(data-scrollx)")
    $sTable_data_sortdirection = $oXml.ChilkatPath("html|body|div[i]|table|(data-sortdirection)")
    $sTable_data_sorton = $oXml.ChilkatPath("html|body|div[i]|table|(data-sorton)")
    $iJ = 0
    $iCount_j = $oXml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr")
    While $iJ < $iCount_j
        $oXml.J = $iJ
        $sTr_role = $oXml.ChilkatPath("html|body|div[i]|table|tbody|tr[j]|(role)")
        $iK = 0
        $iCount_k = $oXml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr[j]|td")
        While $iK < $iCount_k
            $oXml.K = $iK
            $sText = $oXml.GetChildContent("html|body|div[i]|table|tbody|tr[j]|td[k]|text")
            $iK = $iK + 1
        Wend
        $iJ = $iJ + 1
    Wend
    $i = $i + 1
Wend

; Let's modify the above code to build the CSV.
$oCsv = ObjCreate("Chilkat.Csv")
$oCsv.SetColumnName(0,"Ticker")
$oCsv.SetColumnName(1,"Name")
$oCsv.SetColumnName(2,"Sector")
$oCsv.SetColumnName(3,"Country")
$oCsv.SetColumnName(4,"Weight")
$oCsv.SetColumnName(5,"Notional Vaue")

$i = 0
$iCount_i = $oXml.NumChildrenHavingTag("html|body|div")
While $i < $iCount_i
    $oXml.I = $i
    $iJ = 0
    $iCount_j = $oXml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr")
    While $iJ < $iCount_j
        $oXml.J = $iJ
        $iK = 0
        $iCount_k = $oXml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr[j]|td")
        While $iK < $iCount_k
            $oXml.K = $iK
            $oCsv.SetCell($iJ,$iK,$oXml.GetChildContent("html|body|div[i]|table|tbody|tr[j]|td[k]|text"))
            $iK = $iK + 1
        Wend
        $iJ = $iJ + 1
    Wend
    $i = $i + 1
Wend

$oCsv.SaveFile("qa_output/brasil_etf.csv")
Local $sCsvStr = $oCsv.SaveToString()
ConsoleWrite($sCsvStr & @CRLF)

; 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"
;