简体   繁体   中英

Getting specific data from HTML source and showing it in a label

I want to extract specific values from a website that shows me progress within a game, to show them in a vb.net application for easier access. The HTML code I would like to get the data from is:

<div id="freemodeRank" class="page-section clearfix">
    <div class="rankHex right-grad bronze">
        <h3 style="font-size:54px">225</h3>
        <p style="text-align:center;"></p>
    </div>
    <div class="rankXP">
        <div class="clearfix">
            <h3 class="left">5.6M<span> RP</span></h3>
        </div>
        <div class="rankBar">   
            <h4>Play Time: 58d 4h 23m</h4>

The data I would like to get from this code are:

"225", "5.6M" and "Play Time: 58d 4h 23m"

Any help would be great.

You can use a GetBetween function to help you with this.

Public Function GetBetween(ByRef sSearch As String, ByRef sStart As String, ByRef sStop As String, _
                                                Optional ByRef lSearch As Long = 1) As String
    lSearch = InStr(lSearch, sSearch, sStart)
    If lSearch > 0 Then
        lSearch = lSearch + Len(sStart)
        Dim lTemp As Long
        lTemp = InStr(lSearch, sSearch, sStop)
        If lTemp > lSearch Then
            GetBetween = Mid$(sSearch, lSearch, lTemp - lSearch)
        End If
    End If
End Function

Source: http://www.devx.com/tips/Tip/40934

You can call it like this

'Assuming htmlData is the variable where your html string is stored
Dim rpAmount As String = GetBetween(htmlData, "<h3 class=""left"">", "<span> RP</span></h3>")

The same applies the rest of the values you want to scrape

HTML is Xml . Use XDocument class for getting your data. XDocument Class

Dim html As String = "<html><div id=""freemodeRank""><h3>255</h3></div></html>"

Dim document As XDocument = XDocument.Parse(html)
Dim value As String = document.Root...
                               <div>.
                               Where(Function(div) div.@id.Value.Equals("freemodeRank")).
                               <h3>.
                               First().
                               Value

XML Axis Properties (Visual Basic)

LINQ to XML without axis properties

Dim value As String = 
    document.Root.
             Descendants("div").
             Where(Function(div) div.Attribute("id").Value.Equals("freemodeRank")).
             Element("h3").
             Value

Overview of LINQ to XML in Visual Basic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM