好吧,我发现了一个代码,当网站使用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
答案 0 :(得分:0)
src
是img
元素的属性,因此您需要稍微不同地提取它,例如:
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