HTML敏捷包无法抓取图像

时间:2019-02-08 11:49:56

标签: vb.net image html-agility-pack scrape

好吧,我发现了一个代码,当网站使用htmlagility pack vb.net从image声明抓取div时。

我按照程序进行,但一无所获。 这是源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

1 个答案:

答案 0 :(得分:0)

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