Html 敏捷包未能抓取图像

Html agility pack failed to scrape image

好的,我找到了一个代码,该代码作为网站声明使用 htmlagility pack vb.net.

div 抓取 image

我按照程序进行了操作,但一无所获。 这是来源 html:

<div class='my-gallery'>

                    <!-- ONLY PREV NAVIGATION -->
                                        <!-- ONLY PREV NAVIGATION -->

                    <img src='http://example.com/image.jpg' alt='image'/>

                    <!-- ONLY NEXT NAVIGATION -->
                                        <!-- ONLY NEXT NAVIGATION -->

</div>

这是 vb.net 我试过的代码:

Public Sub getImg()


        Try
            Dim link As String = ("http://www.exmple.com")
            'download page from the link into an HtmlDocument'
            Dim doc As HtmlDocument = New HtmlWeb().Load(link)
            Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//div[@class='my-gallery']//img//src")
            If Not div Is Nothing Then              
               PreviewBox.ImageLocation = (div.ToString)
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

srcimg 元素的一个属性,所以你需要稍微不同地提取它,例如:

Dim img As HtmlNode = htmlDocument.DocumentNode.SelectSingleNode("//div[@class='my-gallery']//img")
If img IsNot Nothing Then
    Dim url As String = img.Attributes("src").Value
    PreviewBox.ImageLocation = url
End If