Vb.net HtmlAgilityPack+;Vb+;试着拍张照片
我有我目前的代码来获取标题和图片。 标题在一个文本框中,图片在一个图片框中 在我的windows窗体中,我有:Vb.net HtmlAgilityPack+;Vb+;试着拍张照片,vb.net,Vb.net,我有我目前的代码来获取标题和图片。 标题在一个文本框中,图片在一个图片框中 在我的windows窗体中,我有: Imports System Imports System.Xml Imports HtmlAgilityPack Imports System.Net Imports System.IO Imports System.Collections.Generic 在测试的加载页面中,我有: Public Class scrapper Private Sub scrapper_Lo
Imports System
Imports System.Xml
Imports HtmlAgilityPack
Imports System.Net
Imports System.IO
Imports System.Collections.Generic
在测试的加载页面中,我有:
Public Class scrapper
Private Sub scrapper_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Enable SSL Suppport'
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
'WebPage to Scrapping'
Dim link As String = "https://www.nextinpact.com/"
'download page from the link into an HtmlDocument'
Dim doc As HtmlDocument = New HtmlWeb().Load(link)
'select the title'
Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("/html/body/div[1]/div[2]/section/aside/section/div[2]/div/article[1]/div/div/h3/a")
'select the image'
Dim img As HtmlNode = doc.DocumentNode.SelectSingleNode("/html/body/div[1]/div[2]/div/div[1]/div[5]/div/div[2]/p[1]/a/img")
If Not div Is Nothing Then
TextBox1.Text = div.InnerText.Trim()
End If
If Not img Is Nothing Then
'PictureBox1.Load(img.OuterHtml.Trim())
End If
'Test Picturebox2
PictureBox2.Load("https://cdn2.nextinpact.com/compress/100-76//images/bd/square-linked-media/23647.jpg")
End Sub
End Class
但是在图片中,我无法得到照片
在图2中,这只是为了测试
如何正确获取Picturebox1的图片?那么,只需从元素获取URL即可
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim doc As HtmlDocument = New HtmlWeb().Load("https://www.nextinpact.com/")
PictureBox1.LoadAsync(doc.DocumentNode.SelectSingleNode("//aside[@id='sideBarIndex']//img").Attributes("src").Value)
End Sub
像这样
如果您试图提取PictureBox2中显示的相同图像,则第二个SelectSingleNode上的XPath不正确。我会用这些来代替:
'select the title'
Dim div As HtmlNode = doc.DocumentNode.SelectSingleNode("//aside[@id='sideBarIndex']//article//div/div/h3/a")
'select the image'
Dim img As HtmlNode = doc.DocumentNode.SelectSingleNode("//aside[@id='sideBarIndex']//article//img")
您想从该页面获取哪幅图像?在PictureBox2.LoadGreat的示例中。谢谢你的大力帮助。