以下是使用 C# 编写的简单程序,可以抓取指定网页的 HTML 代码并输出到控制台:

using System;
using System.Net;

class Program
{
    static void Main(string[] args)
    {
        string url = 'https://www.example.com';
        WebClient client = new WebClient();
        string html = client.DownloadString(url);
        Console.WriteLine(html);
        Console.ReadLine();
    }
}

在上面的代码中,我们使用了 C# 中的 WebClient 类来下载网页内容。我们首先创建了一个 WebClient 实例,并将要抓取的网页 URL 传递给 DownloadString 方法。该方法会返回网页的 HTML 代码,我们将其存储在一个字符串变量中,并输出到控制台。

需要注意的是,由于我们使用了 WebClient 类,因此需要添加 System.Net 命名空间。此外,我们还需要在程序中设置对应的网络权限,以允许程序访问互联网。

C# 抓取网页信息代码示例

原文地址: https://www.cveoy.top/t/topic/oXEP 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录