[RESOLVED] Parse HTML Table
Hi all again after a week of trying to figure out how to parse a HTML table I have yet to figure it out.
Below is the Table I am trying to get the information out of.
Code:
<table class="resourceInfoTable">
<tbody>
<tr>
<th>Production</th>
</tr>
<tr>
<td class="fontSize11 fontBold colorWhite">28,900</td>
</tr>
<tr>
<th>Consumption</th>
</tr>
<tr>
<td class="fontSize11 fontBold colorWhite">23,132</td>
</tr>
<tr>
<td class="borderBottom"> </td>
</tr>
<tr>
<th>Yield</th>
</tr>
<tr>
<td class="fontSize16 fontBold fontColorRace">5,768</td>
</tr>
<tr>
<th>Stored</th>
</tr>
<tr>
<td class="fontSize11 fontBold colorWhite">170,000</td>
</tr>
</tbody>
</table>
Below is the code I am using to Pull that information out.
VB Code:
Public Sub Energy()
Dim doc As HtmlDocument = frmWebBrowser.WebBrowser1.Document
Dim tds As HtmlElementCollection = doc.GetElementsByTagName("div")
For Each td As HtmlElement In tds
If td.GetAttribute("id") = "planetEnergyOverview" Then
Dim linkText As String = String.Empty
Dim PlanetClassName As String = String.Empty
For Each div As HtmlElement In td.GetElementsByTagName("table")
If div.GetAttribute("className") = "resourceInfoTable" Then
For Each div2 As HtmlElement In div.GetElementsByTagName("td")
If div2.GetAttribute("className") = "fontSize11 fontBold colorWhite" Then
linkText = div2.InnerText.Trim()
If Not String.IsNullOrEmpty(linkText) Then
Exit For
End If
End If
Next
End If
Next
frmMain.lblEnergyPro.Text = "Production: " & String.Format("{0}", linkText)
End If
Next
End Sub
The problem I am having is that It pulls out the 28,900 fine but I need to pull the rest of the information IE the 23,132 and the 170,000 and they will get placed into other Labels. Now they are not Static numbers they change all the time to higher or lower lumbers.
Thank you for the help in advance this site is helped me get from VB6 to VB.net and learn new things I didn't know I was able to do in VB.
Re: [RESOLVED] Parse HTML Table
Just a little point, you should (ideally) do the if...elseif....elseif...end if block with an else at the end i.e. if...elseif....elseif....else..(something is wrong)...end if not a biggy as the code will exit gracefully if for some reason you got tsCount to be 3, however if it happened you may wanna know so you can adapt the code. this would allow you to know when the data was updated and act accordingly
Re: [RESOLVED] Parse HTML Table
Thanks for the idea Megalith I think I will do that. Yeah getting back into coding has a somewhat steep learning Curve.