溫馨提示×

c# webbrowser怎么獲取網(wǎng)頁內(nèi)容

小億
261
2024-02-01 17:20:34
欄目: 編程語言

要獲取網(wǎng)頁內(nèi)容,你可以使用WebBrowser控件的Document屬性。下面是一個簡單的示例:

using System;
using System.Windows.Forms;

namespace WebBrowserExample
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.Navigate("https://www.example.com");
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            // 獲取網(wǎng)頁內(nèi)容
            string html = webBrowser1.DocumentText;
            Console.WriteLine(html);
        }
    }
}

在上面的示例中,F(xiàn)orm加載時,我們導(dǎo)航到一個URL。然后,當(dāng)網(wǎng)頁加載完成后,我們可以通過DocumentCompleted事件來獲取網(wǎng)頁內(nèi)容。在該事件處理程序中,我們通過DocumentText屬性獲取網(wǎng)頁的HTML內(nèi)容。

0