以下是使用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/huQI 著作权归作者所有。请勿转载和采集!

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