Sample code for 30+ languages & platforms
PowerBuilder

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 PowerBuilder Downloads

PowerBuilder
integer li_rc
long li_Success
oleobject loo_Http
oleobject loo_BdHtml
oleobject loo_Htx
oleobject loo_SbXml
oleobject loo_Xml
long li_NumRemoved
long i
long li_Count_i
string ls_Table_role
string ls_Table_data_scrollx
string ls_Table_data_sortdirection
string ls_Table_data_sorton
string ls_Table_id
long j
long li_Count_j
string ls_Tr_role
long k
long li_Count_k
string ls_TagPath
string ls_Text
oleobject loo_Csv
string ls_CsvStr

li_Success = 0

//  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
loo_Http = create oleobject
li_rc = loo_Http.ConnectToNewObject("Chilkat.Http")
if li_rc < 0 then
    destroy loo_Http
    MessageBox("Error","Connecting to COM object failed")
    return
end if
loo_BdHtml = create oleobject
li_rc = loo_BdHtml.ConnectToNewObject("Chilkat.BinData")

li_Success = loo_Http.QuickGetBd("https://example-code.com/data/etf_table.html",loo_BdHtml)
if li_Success <> 1 then
    Write-Debug loo_Http.LastErrorText
    destroy loo_Http
    destroy loo_BdHtml
    return
end if

//  Convert to XML.
loo_Htx = create oleobject
li_rc = loo_Htx.ConnectToNewObject("Chilkat.HtmlToXml")

loo_Htx.SetHtmlBd(loo_BdHtml)

loo_SbXml = create oleobject
li_rc = loo_SbXml.ConnectToNewObject("Chilkat.StringBuilder")

loo_Htx.ToXmlSb(loo_SbXml)

loo_Xml = create oleobject
li_rc = loo_Xml.ConnectToNewObject("Chilkat.Xml")

loo_Xml.LoadSb(loo_SbXml,1)

//  Remove attributes and sub-trees we don't need.
//  (In other words, we're getting rid of clutter...)
li_NumRemoved = loo_Xml.PruneTag("thead")
li_NumRemoved = loo_Xml.PruneAttribute("style")
li_NumRemoved = loo_Xml.PruneAttribute("class")

//  Scrub the element and attribute content.
loo_Xml.Scrub("ContentTrimEnds,ContentTrimInside,AttrTrimEnds,AttrTrimInside")

//  Let's see what we have...
Write-Debug loo_Xml.GetXml()

//  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:
//  

i = 0
li_Count_i = loo_Xml.NumChildrenHavingTag("html|body|div")
do while i < li_Count_i
    loo_Xml.I = i
    ls_Table_role = loo_Xml.ChilkatPath("html|body|div[i]|div|table|(role)")
    ls_Table_data_scrollx = loo_Xml.ChilkatPath("html|body|div[i]|div|table|(data-scrollx)")
    ls_Table_data_sortdirection = loo_Xml.ChilkatPath("html|body|div[i]|div|table|(data-sortdirection)")
    ls_Table_data_sorton = loo_Xml.ChilkatPath("html|body|div[i]|div|table|(data-sorton)")
    ls_Table_id = loo_Xml.ChilkatPath("html|body|div[i]|table|(id)")
    ls_Table_role = loo_Xml.ChilkatPath("html|body|div[i]|table|(role)")
    ls_Table_data_scrollx = loo_Xml.ChilkatPath("html|body|div[i]|table|(data-scrollx)")
    ls_Table_data_sortdirection = loo_Xml.ChilkatPath("html|body|div[i]|table|(data-sortdirection)")
    ls_Table_data_sorton = loo_Xml.ChilkatPath("html|body|div[i]|table|(data-sorton)")
    j = 0
    li_Count_j = loo_Xml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr")
    do while j < li_Count_j
        loo_Xml.J = j
        ls_Tr_role = loo_Xml.ChilkatPath("html|body|div[i]|table|tbody|tr[j]|(role)")
        k = 0
        li_Count_k = loo_Xml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr[j]|td")
        do while k < li_Count_k
            loo_Xml.K = k
            ls_Text = loo_Xml.GetChildContent("html|body|div[i]|table|tbody|tr[j]|td[k]|text")
            k = k + 1
        loop
        j = j + 1
    loop
    i = i + 1
loop

//  Let's modify the above code to build the CSV.
loo_Csv = create oleobject
li_rc = loo_Csv.ConnectToNewObject("Chilkat.Csv")

loo_Csv.SetColumnName(0,"Ticker")
loo_Csv.SetColumnName(1,"Name")
loo_Csv.SetColumnName(2,"Sector")
loo_Csv.SetColumnName(3,"Country")
loo_Csv.SetColumnName(4,"Weight")
loo_Csv.SetColumnName(5,"Notional Vaue")

i = 0
li_Count_i = loo_Xml.NumChildrenHavingTag("html|body|div")
do while i < li_Count_i
    loo_Xml.I = i
    j = 0
    li_Count_j = loo_Xml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr")
    do while j < li_Count_j
        loo_Xml.J = j
        k = 0
        li_Count_k = loo_Xml.NumChildrenHavingTag("html|body|div[i]|table|tbody|tr[j]|td")
        do while k < li_Count_k
            loo_Xml.K = k
            loo_Csv.SetCell(j,k,loo_Xml.GetChildContent("html|body|div[i]|table|tbody|tr[j]|td[k]|text"))
            k = k + 1
        loop
        j = j + 1
    loop
    i = i + 1
loop

loo_Csv.SaveFile("qa_output/brasil_etf.csv")
ls_CsvStr = loo_Csv.SaveToString()
Write-Debug ls_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"
//  


destroy loo_Http
destroy loo_BdHtml
destroy loo_Htx
destroy loo_SbXml
destroy loo_Xml
destroy loo_Csv