Results 1 to 6 of 6

Thread: [RESOLVED] Extract numbers from HTML Inner Text / Html

  1. #1

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    Resolved [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:
    1. For Each item As HtmlElement In wb.document.all
    2.                 Dim names As New List(Of String)
    3.                 If item.GetAttribute("className") = "green-button tip" Then
    4.                            names.Add(item.innertext)
    5.                 End If
    6.                 GameInfo.LBox2.Items.AddRange(names.ToArray)
    7.             Next

  2. #2
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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:
    1. Dim powerValueList As New List(Of Integer)
    2. For Each parentElement As HtmlElement In myWebBrowser.Document.GetElementsByTagName("a")
    3.   If parentElement.GetAttribute("className") = "green-button tip" Then
    4.     For Each spanElement As HtmlElement In parentElement.GetElementsByTagName("span")
    5.       powerValueList.Add(CInt(spanElement.InnerText))
    6.     Next
    7.   End If
    8. Next
    9. MsgBox(String.Join("/", powerValueList))

    Using LINQ:-
    vb.net Code:
    1. 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
    2. MsgBox(String.Join("/", powerValues))

    Hope that helps.

    Cheers,

    Ian

  3. #3
    Bad man! ident's Avatar
    Join Date
    Mar 2009
    Location
    Cambridge
    Posts
    5,398

    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:
    1. 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

  4. #4
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    Re: Extract numbers from HTML Inner Text / Html

    Nice one too ident,

    I never thought to replace the Where clause with First.

    Ian

  5. #5

    Thread Starter
    Banned
    Join Date
    Jul 2014
    Posts
    221

    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

  6. #6
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,232

    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
  •  



Click Here to Expand Forum to Full Width