0

我试图搜索示例,但似乎没有任何效果。所以我正在使用 HtmlAgilityPack 并且我想获取两个特定标签之间的内部文本。

例子:

<br>Terms of Service<br></br>Developers<br>

我想获得第一个进入 label1<br><br>第二个</br>进入<br>label2的内部文本

这就像

Label1.text = "服务条款"
Label2.text = "开发者"

我如何实现/做/得到这个?附:我对 HtmlAgilityPack 不太熟悉,显示如何执行此操作的代码会做得更好。:-)

谢谢

4

2 回答 2

1

这有点脏,但应该可以。

Imports System.Text.RegularExpressions

  Dim mystring As String = "<br>Terms of Service<br></br>Developers<br>"

    Dim pattern1 As String = "(?<=<br>)(.*?)(?=<br>)"
    Dim pattern2 As String = "(?<=</br>)(.*)(?=<br>)"

    Dim m1 As MatchCollection = Regex.Matches(mystring, pattern1)
    Dim m2 As MatchCollection = Regex.Matches(mystring, pattern2)
    MsgBox(m1(0).ToString)
    MsgBox(m2(0).ToString)
于 2014-01-31T05:07:43.560 回答
0

The short answer is that HAP is not well suited to accomplish your task. My notes below:

Imports HtmlAgilityPack

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim mystring As String = "<BR>Terms of Service<BR></BR>Developers<BR>"
        Dim myDoc As HtmlAgilityPack.HtmlDocument = New HtmlAgilityPack.HtmlDocument
        myDoc.LoadHtml(mystring)
        ' here we notice HAP immediately discards the junk tag </br>
        MsgBox(myDoc.DocumentNode.OuterHtml)

        ' Below we notice that HAP did not close the BR tag because it only 
        ' attempts to close 
        ' certain nested tags associated with tables ( th, tr, td) and lists 
        ' ( li ). 
        ' if this was a supported tag that HAP could fix, the fixed output 
        ' would be as follows: 
        ' <br>Terms of Service<br></br>Developers<br></br></br>
        ' this string would be parsed as if the last tag closes the first 
        ' and each set of 
        ' inner tags close themselves without any text between them. 
        ' This means even if you changed BR to TD, or some other tag HAP 
        ' fixes nesting on, it 
        ' still would not help to parse this correctly.  
        ' Also HAP does not appear to support XHTML in this .net 2.0 version.  

        myDoc.OptionFixNestedTags = True
        MsgBox(myDoc.DocumentNode.OuterHtml)

        ' here we put the BR tag into a collection.  as it iterates through 
        ' the tags we notice there is no inner text on the BR tag, presumably 
        ' because of two reasons.  
        ' 1. HAP will not close a BR.  
        ' 2. It does not fix your broken nested tags as you expect or required.  

        Dim myBR As HtmlNodeCollection = myDoc.DocumentNode.SelectNodes("//BR")
        If Not myBR Is Nothing Then
            For Each br In myBR
                MsgBox(br.InnerText)
            Next
        End If
    End Sub

End Class
于 2014-02-01T07:40:34.050 回答