Results 1 to 3 of 3

Thread: [RESOLVED] Parsing Info from HTML tables

  1. #1

    Thread Starter
    Junior Member simonwinsor's Avatar
    Join Date
    Apr 2008
    Location
    USA
    Posts
    22

    Resolved [RESOLVED] Parsing Info from HTML tables

    Hey vBuddies,

    I have used Pasing before too but not on such a complex scale.
    Html is already extracted from the Website to > "C:\score.txt"
    I wana get the Last Level number (4) and last Scored (301).
    and Show them each in TextBox.

    Please Guide me how do i go about it.

    Here are the HTML codes and Below attached is the Picture how the table looks.

    HTML Code:
    <p class="text"><strong>Game Score :</strong></p>
    
    <table width="50%" border="1" cellspacing="1" cellpadding="2">
      <tr class="text">
        <td width="144" height="20" ><div align="center"><strong>No.</strong></div></td>
        <td width="204" ><div align="center"><strong>To Score</strong></div></td>
        <td width="204" ><div align="center"><strong> Scored</strong></div></td>
        <td width="204" ><div align="center"><strong>Percentage completed</strong></div></td>
        <td width="205" ><div align="center"><strong>Status</strong></div></td>
      </tr>
      <tr class="text">
        <td height="17" ><div align="center">1</div></td>
        <td height="17" ><div align="center">100</div></td>
        <td align="center" valign="middle" >100</td>
        <td align="center" valign="middle" >100.00 %</td>
        <td align="center" valign="middle" > Yes </td>
      </tr>
      <tr class="text">
        <td height="17" ><div align="center">2</div></td>
        <td height="17" ><div align="center">150</div></td>
        <td align="center" valign="middle" >150</td>
        <td align="center" valign="middle" >100.00 %</td>
        <td align="center" valign="middle" > Yes </td>
      </tr>
      <tr class="text">
        <td height="17" ><div align="center">3</div></td>
        <td height="17" ><div align="center">400</div></td>
        <td align="center" valign="middle" >400</td>
        <td align="center" valign="middle" >100.00 %</td>
        <td align="center" valign="middle" > Yes </td>
      </tr>
      <tr class="text">
        <td height="17" ><div align="center">4</div></td>
        <td height="17" ><div align="center">430</div></td>
        <td align="center" valign="middle" >301</td>
        <td align="center" valign="middle" >70.00 %</td>
        <td align="center" valign="middle" > No </td>
      </tr>
    </table>
    Attached Images Attached Images  

  2. #2
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Parsing Info from HTML tables

    This will work with the html you provided
    Code:
    Option Explicit
    
    Private Sub Form_Load()
    Dim fso As FileSystemObject
    Dim ts As TextStream
    Dim strHtml As String
    Dim strSearch As String
    Dim strRows() As String
    Dim strTD() As String
    
        'load the file
        Set fso = New FileSystemObject
        Set ts = fso.OpenTextFile("c:\score.txt")
        
        'extract all the html
        strHtml = ts.ReadAll
        
        'split the html on each row
        strSearch = "<tr class=""text"">"
        strRows = Split(strHtml, strSearch)
        
        'Since you only the last row then pull it from the html
        strHtml = strRows(UBound(strRows))
        
        'Separate the TD tags
        strSearch = "</td>"
        strTD = Split(strHtml, strSearch)
        
        'Display your values
        Text1.Text = GetValue(strTD(0))
        Text2.Text = GetValue(strTD(2))
        
        
        ts.Close
        Set ts = Nothing
        Set fso = Nothing
    End Sub
    
    
    Private Function GetValue(ByVal strLine As String) As String
    Dim strTemp As String
    Dim intPos As Integer
    
        'cleanup the input
        strTemp = Replace(strLine, vbCrLf, "")
        strTemp = Trim(strTemp)
        
        'Remove any tags before the value
        Do While Left(strTemp, 1) = "<"
            intPos = InStr(strTemp, ">") + 1
            strTemp = Mid(strTemp, intPos)
        Loop
        
        'Remove any tags after the value
        intPos = InStr(strTemp, "<")
        If intPos > 0 Then
            strTemp = Left(strTemp, intPos - 1)
        End If
        
        GetValue = strTemp
    End Function

  3. #3

    Thread Starter
    Junior Member simonwinsor's Avatar
    Join Date
    Apr 2008
    Location
    USA
    Posts
    22

    Re: Parsing Info from HTML tables

    Hey MarkT

    Thank you so much for helping me out. The code that you gave worked smoothly.


    Simon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width