Convert HTML <table>'s to CSV files

If the supplied web page has any <TABLE>s they will be converted to CSV file.


' -------------------------------------
' HTML <table> data to CSC file
'  converts all tables in the html
' change: webPage$ and fileName$ below
' --------------------------------------
 
' change this to your web page
webPage$    = "http://www.asx.com.au/asx/markets/optionPrices.do?by=underlyingCode&underlyingCode=FMG&expiryDate=&optionType="
 
' change this to your output csv file name
fileName$    = "myCsv"
 
htmlData$    = httpget$(webPage$)
 
lowData$    = lower$(htmlData$)
open DefaultDir$ + "\public\";fileName$;".csv" for output as #f
 
[nxtTbl]
tblBeg        = instr(lowData$,"<table",tblEnd)
if tblBeg     = 0 then goto [endTbl]
tblEnd        = instr(lowData$,"</table>",tblBeg)
tblData$    = mid$(htmlData$,tblBeg,(tblEnd - tblBeg) + 8)
lineBeg        = 0
lineEnd        = instr(tblData$,"</tr>",lineBeg)
 
while lineEnd > 0
    lineData$    = mid$(tblData$,lineBeg - 1,(lineEnd - lineBeg) + 1)
    lineData$    = strip$(lineData$)
    lineData$    = strRep$(lineData$,"</td>",""",""")
    lineData$    = strRep$(lineData$,"</th>",""",""")
    lineData$    = tags$(lineData$,"")
    lineData$    = strRep$(lineData$,"&nbsp;"," ")
    lineData$    = strRep$(lineData$,"&amp;","and")
 
    print """";left$(lineData$,len(lineData$) -2)
    print #f, """";left$(lineData$,len(lineData$) -2)
 
    lineBeg        = lineEnd + 1
    lineEnd        = instr(tblData$,"</tr>",lineBeg)
wend
print "----------------- end <table> --------------------------"
print #f, ""
goto [nxtTbl]
 
[endTbl]
close #f
print "--------------- That's all folks -----------------------"
wait
 
' -----------------------------------------
' strip junk
' -----------------------------------------
FUNCTION strip$(str$)
    strip$ = ""
    for i = 1 to len(str$)
        a$ = MID$(str$,i,1)
        a = ASC(a$)
        if a > 31 then
                   if a <  127 then
             if a$ <> "'" then
                       if a$ <> """" then
            strip$ = strip$ + a$
               end if
             end if
           end if
        end if
    next i
END FUNCTION
 
' --------------------------------
' string replace rep str with
' --------------------------------
FUNCTION strRep$(str$,rep$,with$)
ln    = len(rep$)
ln1    = ln - 1
i    = 1
while i <= len(str$)
    if mid$(str$,i,ln) = rep$ then
        strRep$ = strRep$ + with$
        i = i + ln1
    else
        strRep$ = strRep$ + mid$(str$,i,1)
    end if
i = i + 1
WEND
END FUNCTION
 
' --------------------------------
' Replace HTML <tags> with$
' --------------------------------
FUNCTION tags$(tag$,with$)
    i    = instr(tag$,"<")
    j    = instr(tag$,">",i)
WHILE j > i
    tag$    = left$(tag$,i - 1) + mid$(tag$,j + 1)
    i    = instr(tag$,"<")
    j    = instr(tag$,">")
WEND
tags$    = tag$
END FUNCTION