PureBasic
PureBasic
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 PureBasic Downloads
IncludeFile "CkBinData.pb"
IncludeFile "CkHttp.pb"
IncludeFile "CkXml.pb"
IncludeFile "CkHtmlToXml.pb"
IncludeFile "CkStringBuilder.pb"
IncludeFile "CkCsv.pb"
Procedure ChilkatExample()
success.i = 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
http.i = CkHttp::ckCreate()
If http.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
bdHtml.i = CkBinData::ckCreate()
If bdHtml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
success = CkHttp::ckQuickGetBd(http,"https://example-code.com/data/etf_table.html",bdHtml)
If success <> 1
Debug CkHttp::ckLastErrorText(http)
CkHttp::ckDispose(http)
CkBinData::ckDispose(bdHtml)
ProcedureReturn
EndIf
; Convert to XML.
htx.i = CkHtmlToXml::ckCreate()
If htx.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHtmlToXml::ckSetHtmlBd(htx,bdHtml)
sbXml.i = CkStringBuilder::ckCreate()
If sbXml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkHtmlToXml::ckToXmlSb(htx,sbXml)
xml.i = CkXml::ckCreate()
If xml.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkXml::ckLoadSb(xml,sbXml,1)
; Remove attributes and sub-trees we don't need.
; (In other words, we're getting rid of clutter...)
numRemoved.i = CkXml::ckPruneTag(xml,"thead")
numRemoved = CkXml::ckPruneAttribute(xml,"style")
numRemoved = CkXml::ckPruneAttribute(xml,"class")
; Scrub the element and attribute content.
CkXml::ckScrub(xml,"ContentTrimEnds,ContentTrimInside,AttrTrimEnds,AttrTrimInside")
; Let's see what we have...
Debug CkXml::ckGetXml(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:
;
i.i
count_i.i
table_role.s
table_data_scrollx.s
table_data_sortdirection.s
table_data_sorton.s
table_id.s
j.i
count_j.i
tr_role.s
k.i
count_k.i
tagPath.s
text.s
i = 0
count_i = CkXml::ckNumChildrenHavingTag(xml,"html|body|div")
While i < count_i
CkXml::setCkI(xml, i)
table_role = CkXml::ckChilkatPath(xml,"html|body|div[i]|div|table|(role)")
table_data_scrollx = CkXml::ckChilkatPath(xml,"html|body|div[i]|div|table|(data-scrollx)")
table_data_sortdirection = CkXml::ckChilkatPath(xml,"html|body|div[i]|div|table|(data-sortdirection)")
table_data_sorton = CkXml::ckChilkatPath(xml,"html|body|div[i]|div|table|(data-sorton)")
table_id = CkXml::ckChilkatPath(xml,"html|body|div[i]|table|(id)")
table_role = CkXml::ckChilkatPath(xml,"html|body|div[i]|table|(role)")
table_data_scrollx = CkXml::ckChilkatPath(xml,"html|body|div[i]|table|(data-scrollx)")
table_data_sortdirection = CkXml::ckChilkatPath(xml,"html|body|div[i]|table|(data-sortdirection)")
table_data_sorton = CkXml::ckChilkatPath(xml,"html|body|div[i]|table|(data-sorton)")
j = 0
count_j = CkXml::ckNumChildrenHavingTag(xml,"html|body|div[i]|table|tbody|tr")
While j < count_j
CkXml::setCkJ(xml, j)
tr_role = CkXml::ckChilkatPath(xml,"html|body|div[i]|table|tbody|tr[j]|(role)")
k = 0
count_k = CkXml::ckNumChildrenHavingTag(xml,"html|body|div[i]|table|tbody|tr[j]|td")
While k < count_k
CkXml::setCkK(xml, k)
text = CkXml::ckGetChildContent(xml,"html|body|div[i]|table|tbody|tr[j]|td[k]|text")
k = k + 1
Wend
j = j + 1
Wend
i = i + 1
Wend
; Let's modify the above code to build the CSV.
csv.i = CkCsv::ckCreate()
If csv.i = 0
Debug "Failed to create object."
ProcedureReturn
EndIf
CkCsv::ckSetColumnName(csv,0,"Ticker")
CkCsv::ckSetColumnName(csv,1,"Name")
CkCsv::ckSetColumnName(csv,2,"Sector")
CkCsv::ckSetColumnName(csv,3,"Country")
CkCsv::ckSetColumnName(csv,4,"Weight")
CkCsv::ckSetColumnName(csv,5,"Notional Vaue")
i = 0
count_i = CkXml::ckNumChildrenHavingTag(xml,"html|body|div")
While i < count_i
CkXml::setCkI(xml, i)
j = 0
count_j = CkXml::ckNumChildrenHavingTag(xml,"html|body|div[i]|table|tbody|tr")
While j < count_j
CkXml::setCkJ(xml, j)
k = 0
count_k = CkXml::ckNumChildrenHavingTag(xml,"html|body|div[i]|table|tbody|tr[j]|td")
While k < count_k
CkXml::setCkK(xml, k)
CkCsv::ckSetCell(csv,j,k,CkXml::ckGetChildContent(xml,"html|body|div[i]|table|tbody|tr[j]|td[k]|text"))
k = k + 1
Wend
j = j + 1
Wend
i = i + 1
Wend
CkCsv::ckSaveFile(csv,"qa_output/brasil_etf.csv")
csvStr.s = CkCsv::ckSaveToString(csv)
Debug 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"
;
CkHttp::ckDispose(http)
CkBinData::ckDispose(bdHtml)
CkHtmlToXml::ckDispose(htx)
CkStringBuilder::ckDispose(sbXml)
CkXml::ckDispose(xml)
CkCsv::ckDispose(csv)
ProcedureReturn
EndProcedure