VB .NET HTMLAgilityPack冒号分隔值

时间:2016-12-22 02:06:43

标签: vb.net parsing html-agility-pack

有没有办法使用HTMLAgilityPack获取标记内的值? 我的变量Dim height As Double Dim weight As Double Dim gender As String Dim newWeight As Double Dim lastrow As Long lastrow = Cells(Rows.Count, 1).End(xlUp).row For i = 2 To lastrow 'I'd like to set the loop here 'for each row do this height = CDbl(Range("D" & i).value) weight = CDbl(Range("F" & i).value) gender = Range("K" & i).value 'then run through my equations If gender = "F" And weight < 20 Then newWeight = weight + 5 ElseIf gender = "F" And weight > 20 Then newWeight = weight - 2 End If Range("I" & i).value = newWeight 'Then clear variables and run through the new row newWeight = 0 height = 0 weight = 0 gender = 0 Next dataNode,其中包含:

HtmlAgilityPack.HtmlNode

希望获得每个Dim doc as New HtmlAgilityPack.HtmlDocument() doc.LoadHtml(" <div id="container" data="id:12,country:usa,city:oregon,id:13,country:usa,city:atlanta"> <a href="http://www.google.com">Google</a> </div> ") idcountry的价值。它们在标签内重复并具有不同的值。

city

这会产生错误Dim dataNode as HtmlAgililtyPack.HtmlNode dataNode = doc.documentNode.SelectSingleNode("//div") txtbox.text = dataNode.Attributes("id[1]").value

1 个答案:

答案 0 :(得分:0)

您需要"data"属性,而不是"id"属性。

获得正确属性的值后,您需要将其解析为适合保存数据的每个部分的某些数据结构,例如:

Option Infer On
Option Strict On

Module Module1

    Public Class LocationDatum
        Property ID As Integer
        Property Country As String
        Property City As String

        Public Overrides Function ToString() As String
            Return $"ID={ID}, Country={Country}, City={City}"
        End Function

    End Class


    Sub Main()
        Dim doc As New HtmlAgilityPack.HtmlDocument()

        doc.LoadHtml("
<div id=""container"" data=""id:12,country:usa,city:oregon,id:13,country:usa,city:atlanta"">
    <a href=""http://www.google.com"">Google</a>
</div>
")

        Dim dataNode = doc.DocumentNode.SelectSingleNode("//div")
        Dim rawData = dataNode.Attributes("data").Value
        Dim dataParts = rawData.Split(","c)

        Dim locationData As New List(Of LocationDatum)

        ' A simple way of parsing the data
        For i = 0 To dataParts.Count - 1 Step 3
            If i + 2 < dataParts.Count Then
                Dim id As Integer = -1
                Dim country As String = ""
                Dim city As String = ""
                ' used to check all three required parts have been found:
                Dim partsFoundFlags = 0
                For j = 0 To 2
                    Dim itemParts = dataParts(i + j).Split(":"c)
                    Select Case itemParts(0)
                        Case "id"
                            id = CInt(itemParts(1))
                            partsFoundFlags = partsFoundFlags Or 1
                        Case "country"
                            country = itemParts(1)
                            partsFoundFlags = partsFoundFlags Or 2
                        Case "city"
                            city = itemParts(1)
                            partsFoundFlags = partsFoundFlags Or 4
                    End Select
                Next
                If partsFoundFlags = 7 Then
                    locationData.Add(New LocationDatum With {.ID = id, .Country = country, .City = city})
                End If
            End If

        Next

        For Each d In locationData
            Console.WriteLine(d)
        Next

        Console.ReadLine()

    End Sub

End Module

哪个输出:

  

ID = 12,Country = usa,City = oregon
  ID = 13,国家=美国,城市=亚特兰大

它可以抵抗某些数据错误,例如id / city / country的顺序不同,以及最后的虚假数据。

当然,您可以将解析代码放入其自己的函数中。