-
Jul 19th, 2014, 09:15 PM
#1
Thread Starter
Banned
[RESOLVED] Extract numbers from HTML Inner Text / Html
HTML Code:
<a class="green-button tip">
<div class="tooltip-html">
<h2>Nidalee</h2>
<p><b>21</b> Offense
<br><b>0</b> Defense
<br><b>9</b> Utility</p>
<p>Note: Click button for full mastery tree.</p>
</div>
<span class="offense">21</span>/<span class="defense">0</span>/<span class="utility">9</span>
</a>
How can I extract number 21/0/9 from the innerhtml of "green-button tip" editing the code I already have
vb.net Code:
For Each item As HtmlElement In wb.document.all
Dim names As New List(Of String)
If item.GetAttribute("className") = "green-button tip" Then
names.Add(item.innertext)
End If
GameInfo.LBox2.Items.AddRange(names.ToArray)
Next
-
Jul 19th, 2014, 11:24 PM
#2
Re: Extract numbers from HTML Inner Text / Html
Hi,
The first thing to do is to fix that HTML snippet that you posted since it is missing some of its "br" closing tags. Once done, here are two examples for you:-
Using traditional For loops:-
vb.net Code:
Dim powerValueList As New List(Of Integer)
For Each parentElement As HtmlElement In myWebBrowser.Document.GetElementsByTagName("a")
If parentElement.GetAttribute("className") = "green-button tip" Then
For Each spanElement As HtmlElement In parentElement.GetElementsByTagName("span")
powerValueList.Add(CInt(spanElement.InnerText))
Next
End If
Next
MsgBox(String.Join("/", powerValueList))
Using LINQ:-
vb.net Code:
Dim powerValues() As Integer = myWebBrowser.Document.GetElementsByTagName("a").Cast(Of HtmlElement).Where(Function(x) x.GetAttribute("className") = "green-button tip").First.GetElementsByTagName("span").Cast(Of HtmlElement).Select(Function(x) CInt(x.InnerText)).ToArray
MsgBox(String.Join("/", powerValues))
Hope that helps.
Cheers,
Ian
-
Jul 20th, 2014, 04:13 AM
#3
Re: Extract numbers from HTML Inner Text / Html
Good job Ian. The only thing i would change is the LINQ where. We can remove this and simply short circuit using First/FirstOrDefault
vb Code:
Dim powerValues() As Integer = Me.WebBrowser1.Document.GetElementsByTagName("a").Cast(Of HtmlElement).First(Function(x) x.GetAttribute("className") = "green-button tip").GetElementsByTagName("span").Cast(Of HtmlElement).Select(Function(x) CInt(x.InnerText)).ToArray
-
Jul 20th, 2014, 05:06 AM
#4
Re: Extract numbers from HTML Inner Text / Html
Nice one too ident,
I never thought to replace the Where clause with First.
Ian
-
Jul 20th, 2014, 08:28 AM
#5
Thread Starter
Banned
Re: Extract numbers from HTML Inner Text / Html
LINQ is some kind of a short code or what? Btw ty. Ian. I'll use the first code I actually understand xD
-
Jul 20th, 2014, 08:43 AM
#6
Re: Extract numbers from HTML Inner Text / Html
Hi,
I made sure that I posted a traditional For loop example as well as a LINQ example to make sure that you would be able to understand whichever technology you choose to use in .NET. LINQ was introduced in VS2008 and is a very powerful tool once you get used to it but it does take some "getting your head round it".
Have a read here if you would like to learn more:-
LINQ (Language-Integrated Query)
Cheers,
Ian
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|