Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text
HTTP

HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
Secrets
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) HTML Table to CSV

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

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

Chilkat ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoHttp
    Variant vBdHtml
    Handle hoBdHtml
    Boolean iSuccess
    Handle hoHtx
    Variant vSbXml
    Handle hoSbXml
    Handle hoXml
    Integer iNumRemoved
    Integer i
    Integer iCount_i
    String sTable_role
    String sTable_data_scrollx
    String sTable_data_sortdirection
    String sTable_data_sorton
    String sTable_id
    Integer j
    Integer iCount_j
    String sTr_role
    Integer k
    Integer iCount_k
    String sTagPath
    String sText
    Handle hoCsv
    String sCsvStr
    String sTemp1

    // 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
    Get Create (RefClass(cComChilkatHttp)) To hoHttp
    If (Not(IsComObjectCreated(hoHttp))) Begin
        Send CreateComObject of hoHttp
    End
    Get Create (RefClass(cComChilkatBinData)) To hoBdHtml
    If (Not(IsComObjectCreated(hoBdHtml))) Begin
        Send CreateComObject of hoBdHtml
    End

    Get pvComObject of hoBdHtml to vBdHtml
    Get ComQuickGetBd Of hoHttp "https://example-code.com/data/etf_table.html" vBdHtml To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoHttp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    // Convert to XML.
    Get Create (RefClass(cComChilkatHtmlToXml)) To hoHtx
    If (Not(IsComObjectCreated(hoHtx))) Begin
        Send CreateComObject of hoHtx
    End
    Get pvComObject of hoBdHtml to vBdHtml
    Get ComSetHtmlBd Of hoHtx vBdHtml To iSuccess

    Get Create (RefClass(cComChilkatStringBuilder)) To hoSbXml
    If (Not(IsComObjectCreated(hoSbXml))) Begin
        Send CreateComObject of hoSbXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComToXmlSb Of hoHtx vSbXml To iSuccess

    Get Create (RefClass(cComChilkatXml)) To hoXml
    If (Not(IsComObjectCreated(hoXml))) Begin
        Send CreateComObject of hoXml
    End
    Get pvComObject of hoSbXml to vSbXml
    Get ComLoadSb Of hoXml vSbXml True To iSuccess

    // Remove attributes and sub-trees we don't need.
    // (In other words, we're getting rid of clutter...)
    Get ComPruneTag Of hoXml "thead" To iNumRemoved
    Get ComPruneAttribute Of hoXml "style" To iNumRemoved
    Get ComPruneAttribute Of hoXml "class" To iNumRemoved

    // Scrub the element and attribute content.
    Send ComScrub To hoXml "ContentTrimEnds,ContentTrimInside,AttrTrimEnds,AttrTrimInside"

    // Let's see what we have...
    Get ComGetXml Of hoXml To sTemp1
    Showln sTemp1

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

    Move 0 To i
    Get ComNumChildrenHavingTag Of hoXml "html|body|div" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoXml To i
        Get ComChilkatPath Of hoXml "html|body|div[i]|div|table|(role)" To sTable_role
        Get ComChilkatPath Of hoXml "html|body|div[i]|div|table|(data-scrollx)" To sTable_data_scrollx
        Get ComChilkatPath Of hoXml "html|body|div[i]|div|table|(data-sortdirection)" To sTable_data_sortdirection
        Get ComChilkatPath Of hoXml "html|body|div[i]|div|table|(data-sorton)" To sTable_data_sorton
        Get ComChilkatPath Of hoXml "html|body|div[i]|table|(id)" To sTable_id
        Get ComChilkatPath Of hoXml "html|body|div[i]|table|(role)" To sTable_role
        Get ComChilkatPath Of hoXml "html|body|div[i]|table|(data-scrollx)" To sTable_data_scrollx
        Get ComChilkatPath Of hoXml "html|body|div[i]|table|(data-sortdirection)" To sTable_data_sortdirection
        Get ComChilkatPath Of hoXml "html|body|div[i]|table|(data-sorton)" To sTable_data_sorton
        Move 0 To j
        Get ComNumChildrenHavingTag Of hoXml "html|body|div[i]|table|tbody|tr" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoXml To j
            Get ComChilkatPath Of hoXml "html|body|div[i]|table|tbody|tr[j]|(role)" To sTr_role
            Move 0 To k
            Get ComNumChildrenHavingTag Of hoXml "html|body|div[i]|table|tbody|tr[j]|td" To iCount_k
            While (k < iCount_k)
                Set ComK Of hoXml To k
                Get ComGetChildContent Of hoXml "html|body|div[i]|table|tbody|tr[j]|td[k]|text" To sText
                Move (k + 1) To k
            Loop

            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop

    // Let's modify the above code to build the CSV.
    Get Create (RefClass(cComChilkatCsv)) To hoCsv
    If (Not(IsComObjectCreated(hoCsv))) Begin
        Send CreateComObject of hoCsv
    End
    Get ComSetColumnName Of hoCsv 0 "Ticker" To iSuccess
    Get ComSetColumnName Of hoCsv 1 "Name" To iSuccess
    Get ComSetColumnName Of hoCsv 2 "Sector" To iSuccess
    Get ComSetColumnName Of hoCsv 3 "Country" To iSuccess
    Get ComSetColumnName Of hoCsv 4 "Weight" To iSuccess
    Get ComSetColumnName Of hoCsv 5 "Notional Vaue" To iSuccess

    Move 0 To i
    Get ComNumChildrenHavingTag Of hoXml "html|body|div" To iCount_i
    While (i < iCount_i)
        Set ComI Of hoXml To i
        Move 0 To j
        Get ComNumChildrenHavingTag Of hoXml "html|body|div[i]|table|tbody|tr" To iCount_j
        While (j < iCount_j)
            Set ComJ Of hoXml To j
            Move 0 To k
            Get ComNumChildrenHavingTag Of hoXml "html|body|div[i]|table|tbody|tr[j]|td" To iCount_k
            While (k < iCount_k)
                Set ComK Of hoXml To k
                Get ComGetChildContent Of hoXml "html|body|div[i]|table|tbody|tr[j]|td[k]|text" To sTemp1
                Get ComSetCell Of hoCsv j k sTemp1 To iSuccess
                Move (k + 1) To k
            Loop

            Move (j + 1) To j
        Loop

        Move (i + 1) To i
    Loop

    Get ComSaveFile Of hoCsv "qa_output/brasil_etf.csv" To iSuccess
    Get ComSaveToString Of hoCsv To sCsvStr
    Showln sCsvStr

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


End_Procedure

 

© 2000-2024 Chilkat Software, Inc. All Rights Reserved.