Sunday, February 13, 2011

Vbscript: Parsing WorldofLogs.com - Part 2 Parsing Html Tables

sDir = "data\"

Set oRe = CreateObject("vbscript.regexp")
Set oFs = CreateObject( "Scripting.FileSystemObject" )
Set oFileOut = oFs.OpenTextFile("test1.csv",2,True)

If sDir <> "" Then
If oFs.FolderExists( sDir ) Then
Set oFolder = oFs.GetFolder( sDir )
Set oFiles = oFolder.Files
For Each oFile In oFiles
sNewColumns = Replace( oFile.Name, "-", "," )
sNewColumns = Replace( sNewColumns, ".txt", "" )
Set oFileIn = oFs.OpenTextFile( sDir & oFile.Name, 1 )
If oFileIn.AtEndOfStream Then
WScript.Echo "File contains no data: " & oFile.Name
sData = ""
Else
sData = oFileIn.ReadAll

With oRe
.IgnoreCase = True
.Global = True
.MultiLine = True

.Pattern = "<table class='playerRankMixed'>([\S\s]*?)</table>"
Test = oRe.Test( sData )
If Test Then
Set Matches = .Execute( sData )
sTable = Matches(0).SubMatches(0)

sTable = Replace(sTable,"<th","<td")
sTable = Replace(sTable,"</th>","</td>")
.Pattern = "<tr([\S\s]*?)</tr>"
Set Rows = .Execute(sTable)
'ReDim aRow(Rows.Count)
'i = 0
For Each Row In Rows
' aRow(i) = Row.Value
' i = i + 1
'Next
'For Each r In aRow

oFileOut.Write( sNewColumns )

.Pattern = "<td([\S\s]*?)</td>"
Set Cols = .Execute( Row.Value )'r)
For Each Col In Cols

' Remove Crap Html Code
.Pattern = "</?\w+((\s+\w+(\s*=\s*(?:"".*?""|'.*?'|[^'"">\s]+))?)+\s*|\s*)/?>"
sNoHtml = Trim( .Replace( Col.Value, "" ) )
oFileOut.Write( "," & sNoHtml )

Next
oFileOut.WriteLine()
Next
End If
End With
End If
WScript.Echo sNewColumns
' tenStop = tenStop + 1: If tenStop > 9 Then Wscript.Echo "Exit Early!": Wscript.Quit
Next
Else
WScript.Echo "Directory does not exist: " & sDir
End If
Else
WScript.Echo "No directory specified: " & sDir
End If
Set oFs = Nothing

WScript.Echo "Done!"
WScript.Quit

No comments: