VB.NET Web浏览器到文本框

发布于 2024-12-06 05:38:13 字数 218 浏览 0 评论 0原文

我需要帮助将所有文本从 WebBrowser1 获取到我的 textbox1.text

我尝试将

    WebBrowser1.Navigate(TextBox3.Text)
    TextBox1.Text = WebBrowser1.DocumentText

textbox3 作为我的网站 textbox1 是我想要的所有文本。

I need help getting all of the text from WebBrowser1 to my textbox1.text

I tried

    WebBrowser1.Navigate(TextBox3.Text)
    TextBox1.Text = WebBrowser1.DocumentText

textbox3 being my website
and textbox1 being were i want all the text.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2024-12-13 05:38:13

您必须处理 WebBrowser 控件的 DocumentCompleted 事件。

Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, 
       e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 
              Handles WebBrowser1.DocumentCompleted
  TextBox1.Text = WebBrowser1.DocumentText 
End Sub

You have to handle the DocumentCompleted event of WebBrowser control.

Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, 
       e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) 
              Handles WebBrowser1.DocumentCompleted
  TextBox1.Text = WebBrowser1.DocumentText 
End Sub
萌无敌 2024-12-13 05:38:13

我的解决方案:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' WebBrowser1
        ' TextBox1
        ' TextBox2
        '
        WebBrowser1.ScriptErrorsSuppressed = True      ' we would like to suppress scripts error message
        WebBrowser1.Navigate("http://codeguru.com")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ' 1) Get entire html code and save as .html file
        TextBox1.Text = WebBrowser1.DocumentText

        ' HOWTO retry while error UNTIL ok
        ' this needs to be done because Body.InnerText returns error when called too soon
        ' 2) Get Body text and save as .txt file
        Dim retry As Boolean = True
        Dim body As String = ""
        While retry
            Try
                body = WebBrowser1.Document.Body.InnerText
                retry = False
            Catch ex As System.IO.IOException
                retry = True
            Finally
                TextBox2.Text = body
            End Try
        End While

    End Sub
End Class

My solution:

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' WebBrowser1
        ' TextBox1
        ' TextBox2
        '
        WebBrowser1.ScriptErrorsSuppressed = True      ' we would like to suppress scripts error message
        WebBrowser1.Navigate("http://codeguru.com")

    End Sub

    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        ' 1) Get entire html code and save as .html file
        TextBox1.Text = WebBrowser1.DocumentText

        ' HOWTO retry while error UNTIL ok
        ' this needs to be done because Body.InnerText returns error when called too soon
        ' 2) Get Body text and save as .txt file
        Dim retry As Boolean = True
        Dim body As String = ""
        While retry
            Try
                body = WebBrowser1.Document.Body.InnerText
                retry = False
            Catch ex As System.IO.IOException
                retry = True
            Finally
                TextBox2.Text = body
            End Try
        End While

    End Sub
End Class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文