从HTML源获取特定数据并将其显示在标签中

时间:2016-07-16 23:17:42

标签: html vb.net

我想从一个显示游戏进度的网站中提取特定值,以便在vb.net应用程序中显示它们以便于访问。我想从中获取数据的HTML代码是:

<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>

我想从这段代码中得到的数据是:

&#34; 225&#34;,&#34; 5.6M&#34;和&#34;播放时间:58d 4h 23m&#34;

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:0)

您可以使用GetBetween函数来帮助您解决此问题。

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

来源:http://www.devx.com/tips/Tip/40934

你可以这样称呼它

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

同样适用于你想要刮去的其余值

答案 1 :(得分:0)

HTML是Xml。使用XDocument课程获取数据。 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

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