【发布时间】:2021-03-27 17:05:54
【问题描述】:
我想使用 HTML 敏捷包以 Windows 形式提取 WebBrowser 活动中的字段/文本。我可以在后台抓取文本,但想在表单内的 WebBrowser 中进行。
我尝试将我的 HtmlDocument 变量引用到 WebBrowser1.Document 但似乎无法转换它。
这是我遇到的错误
这些是变量类型
这是我的代码。
Imports System
Imports System.Xml
Imports HtmlAgilityPack
Public Class Form1
Private Sub Form1_load(sender As System.Object, e As EventArgs) Handles MyBase.Load
WebBrowser1.Navigate(TextBox3.Text)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim link As String = TextBox3.Text
Dim doc As HtmlDocument = New HtmlWeb().Load(link)
Dim web_document As HtmlDocument = WebBrowser1.Document
Dim name As HtmlNode = doc.DocumentNode.SelectSingleNode("//*[@id='details']/div[2]/div[2]/div/div[1]/h3")
'if the div is found, print the inner text'
If Not name Is Nothing Then
TextBox1.Text = name.InnerText.Trim()
End If
Dim customer_number As HtmlNode = doc.DocumentNode.SelectSingleNode("//*[@id='details']/div[2]/div[2]/div/div[2]/dl[4]/dd")
'if the div is found, print the inner text'
If Not customer_number Is Nothing Then
TextBox2.Text = customer_number.InnerText.Trim()
End If
MessageBox.Show("Doc variable: " + doc.GetType.ToString + Environment.NewLine + "web_document variable: " + web_document.GetType.ToString)
End Sub
Private Sub WebBrowser1_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
End Sub
End Class
【问题讨论】:
标签: vb.net variables web-scraping html-agility-pack