【问题标题】:HtmlAgilityPack - SelectNodesHtmlAgilityPack - 选择节点
【发布时间】:2018-01-10 16:27:50
【问题描述】:

我正在尝试检索 <p class> 元素。

<div class="thread-plate__details">
    <h3 class="thread-plate__title">(S) HexHunter BOW</h3>
    <p class="thread-plate__summary">created by Aazoth</p>  <!-- (THIS ONE) -->
</div>

但没有运气。

我使用的代码如下:

' the example url to scrape
            Dim url As String = "http://services.runescape.com/m=forum/forums.ws?39,40,goto," & Label6.Text
            Dim source As String = GetSource(url)

            If source IsNot Nothing Then
                ' create a new html document and load the pages source
                Dim htmlDocument As New HtmlDocument
                htmlDocument.LoadHtml(source)

                ' Create a new collection of all href tags
                Dim nodes As HtmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//p[@class]")

                ' Using LINQ get all href values that start with http://
                ' of course there are others such as www.
                Dim links =
                    (
                        From node
                        In nodes
                        Let attribute = node.Attributes("class")
                        Where attribute.Value.StartsWith("created by ")
                        Select attribute.Value
                    )

                Me.ListBox1a.Items.AddRange(links.ToArray)
                Dim o, j As Long
                For o = 0 To ListBox1a.Items.Count - 1
                    For j = ListBox1a.Items.Count - 1 To (o + 1) Step -1
                        If ListBox1a.Items(o) = ListBox1a.Items(j) Then
                            ListBox1a.Items.Remove(ListBox1a.Items((j)))
                        End If
                    Next
                Next
                For i As Integer = 0 To Me.ListBox1a.Items.Count - 1
                    Me.ListBox1a.Items(i) = Me.ListBox1a.Items(i).ToString.Replace("created by ", "")

                Next

                For Each s As String In ListBox1a.Items
                    Dim lvi As New NetSeal.NSListView
                    lvi.Text = s
                    NsListView1.Items.Add(lvi.Text)

                Next

它可以运行,但我无法获得“由 XXX 创建”的文本。 我尝试了很多方法,但都没有运气,将不胜感激。

提前谢谢大家。

【问题讨论】:

    标签: vb.net html-agility-pack


    【解决方案1】:

    看起来您在attribute.Value 中查找错误的字符串。我看到的是attribute.Value.StartsWith("created by ")必须改成这个attribute.Value.StartsWith("thread-plate__summary")

    要获取节点的内部内容,您必须这样做:Select node.InnerText;

    ' the example url to scrape
    Dim url As String = "http://services.runescape.com/m=forum/forums.ws?39,40,goto," & Label6.Text
    Dim source As String = GetSource(url)
    
    If source IsNot Nothing Then
        ' create a new html document and load the pages source
        Dim htmlDocument As New HtmlDocument
        htmlDocument.LoadHtml(source)
    
        ' Create a new collection of all href tags
        Dim nodes As HtmlNodeCollection = htmlDocument.DocumentNode.SelectNodes("//p[@class]")
    
        ' Using LINQ get all href values that start with http://
        ' of course there are others such as www.
        Dim links =
            (
                From node
                In nodes
                Let attribute = node.Attributes("class")
                Where attribute.Value.StartsWith("thread-plate__summary")
                Select node.InnerText
            )
    
        Me.ListBox1a.Items.AddRange(links.ToArray)
        Dim o, j As Long
        For o = 0 To ListBox1a.Items.Count - 1
            For j = ListBox1a.Items.Count - 1 To (o + 1) Step -1
                If ListBox1a.Items(o) = ListBox1a.Items(j) Then
                    ListBox1a.Items.Remove(ListBox1a.Items((j)))
                End If
            Next
        Next
        For i As Integer = 0 To Me.ListBox1a.Items.Count - 1
            Me.ListBox1a.Items(i) = Me.ListBox1a.Items(i).ToString.Replace("created by ", "")
    
        Next
    
        For Each s As String In ListBox1a.Items
            Dim lvi As New NetSeal.NSListView
            lvi.Text = s
            NsListView1.Items.Add(lvi.Text)
    
        Next
    

    我希望这对你有用。

    【讨论】:

    • 感谢安迪,但没有奏效。输出是 thread-plate__summary 而不是我想要的文本。
    • 成功了,安迪,非常感谢您快速有效的回答!
    • Select node.InnerText 顶部的 Select attribute.Value & ":" & attribute.Value 出现错误。
    • 你想通过这个实现什么?此外,Select Case 的使用方式错误。在这里,您可以阅读有关如何在 linq 中使用案例技术的更多信息:sankarsan.wordpress.com/2010/05/16/…
    • 我正在尝试实现这一目标i.imgur.com/nx1IIvk.png,但每当我放置 Select node.InnerText 时,它都会给我一个错误。如您所见,没有 Select node.InnerText 它工作得很好。没有选择案例i.imgur.com/o7gaK61.png
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-06
    • 1970-01-01
    • 2014-10-17
    • 2014-09-04
    • 2012-07-02
    相关资源
    最近更新 更多