Search code examples
vb.netimageweb-scrapinghtml-agility-pack

Html agility pack failed to scrape image


Ok I have found a code which as the website declared scrape image from div using htmlagility pack vb.net.

I followed the procedure and I get nothing. This is source 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>

This is vb.net code I tried:

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

Solution

  • The src is an attribute of the img element, so you need to extract it slightly differently, for example:

    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