C#编写HttpClient爬虫程序示例

要写一个使用C#和HttpClient的爬虫程序。首先,我需要了解HttpClient的基本用法。HttpClient是用来发送HTTP请求和接收响应的类,对吧?我记得在C#中使用它的时候需要注意一些事情,比如最好使用单例实例,而不是频繁创建和销毁,这样可以避免端口耗尽的问题。

以下是使用C#和HttpClient编写的爬虫程序示例,包含详细注释和扩展说明:

csharp 复制代码
using System;
using System.Net.Http;
using System.Threading.Tasks;
using HtmlAgilityPack; // 需要安装NuGet包

class WebCrawler
{
    private static readonly HttpClient _httpClient = new HttpClient();

    static async Task Main(string[] args)
    {
        // 配置HTTP客户端
        _httpClient.DefaultRequestHeaders.UserAgent.ParseAdd("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36");
        _httpClient.Timeout = TimeSpan.FromSeconds(30);

        try
        {
            string url = "https://example.com";
            
            // 发送HTTP GET请求
            HttpResponseMessage response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode(); // 检查状态码

            // 读取响应内容
            string htmlContent = await response.Content.ReadAsStringAsync();
            
            // 解析HTML内容
            var htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(htmlContent);

            // 示例:提取所有链接
            foreach (HtmlNode link in htmlDoc.DocumentNode.SelectNodes("//a[@href]"))
            {
                string hrefValue = link.GetAttributeValue("href", string.Empty);
                Console.WriteLine($"发现链接: {hrefValue}");
            }

            // 示例:提取页面标题
            string pageTitle = htmlDoc.DocumentNode.SelectSingleNode("//title")?.InnerText;
            Console.WriteLine($"页面标题: {pageTitle}");
        }
        catch (HttpRequestException ex)
        {
            Console.WriteLine($"HTTP请求失败: {ex.Message}");
        }
        catch (TaskCanceledException ex)
        {
            Console.WriteLine($"请求超时: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生错误: {ex.Message}");
        }
    }
}

关键组件说明

  1. HttpClient配置

    • 单例模式:避免短时间创建多个实例导致端口耗尽
    • 用户代理:模拟浏览器行为避免被屏蔽
    • 超时设置:30秒请求超时限制
  2. HTML解析

    • 使用HtmlAgilityPack(需通过NuGet安装)
    • XPath查询语法提取特定元素
  3. 异常处理

    • 专用HTTP异常处理
    • 超时单独处理
    • 通用异常捕获

扩展增强建议

  1. 代理支持

    csharp 复制代码
    var handler = new HttpClientHandler
    {
        Proxy = new WebProxy("http://proxy-server:8080"),
        UseProxy = true
    };
    var client = new HttpClient(handler);
  2. 请求限速

    csharp 复制代码
    await Task.Delay(TimeSpan.FromSeconds(2)); // 每次请求间隔
  3. Cookie管理

    csharp 复制代码
    var cookieContainer = new CookieContainer();
    var handler = new HttpClientHandler { CookieContainer = cookieContainer };
  4. 内容过滤

    csharp 复制代码
    if (hrefValue.EndsWith(".pdf")) // 过滤PDF文件

注意事项

  1. 法律合规

    • 遵守目标网站的robots.txt协议
    • 尊重版权和隐私条款
  2. 性能优化

    • 使用HttpClientFactoryASP.NET Core环境)
    • 并行请求控制
  3. 反爬对策

    • 随机化请求间隔
    • 使用代理池轮换
    • 处理验证码(需要额外服务)

安装HtmlAgilityPack:

bash 复制代码
dotnet add package HtmlAgilityPack

注意:C# 7.1+ 支持异步Main方法,需在.csproj中添加:

xml 复制代码
<PropertyGroup>
<LangVersion>latest</LangVersion>
</PropertyGroup>

这个爬虫框架可根据具体需求扩展更多功能,建议在实际使用中遵守目标网站的服务条款和相关法律法规。

相关推荐
sycmancia3 分钟前
Qt——对话框及其类型
开发语言·qt
IP老炮不瞎唠7 分钟前
为什么Python爬虫需要代理 IP?原理与应用详解
爬虫·python·tcp/ip
趙卋傑9 分钟前
测试开发场景下常见的 MCP 服务
开发语言·python·测试工具·ai编程
@atweiwei9 分钟前
langchainrust:Rust 版 LangChain 框架(LLM+Agent+RAG)
开发语言·rust·langchain·agent·向量数据库·rag
阿里嘎多学长11 分钟前
2026-04-11 GitHub 热点项目精选
开发语言·程序员·github·代码托管
yugi98783812 分钟前
基于最大信息熵的粒子群优化算法图像分割(MATLAB实现)
开发语言·算法·matlab
yaoxin52112312 分钟前
376. Java IO API - 使用 Globbing 和自定义 Filter 过滤目录内容
java·开发语言·python
lifallen20 分钟前
Flink Agents:Python 执行链路与跨语言 Actor (PyFlink Agent)
java·大数据·人工智能·python·语言模型·flink
飞翔的SA20 分钟前
全程 Python:无需离开 Python 即可实现光速级 CUDA 加速,无需c++支持
开发语言·c++·python·nvidia·cuda
冰暮流星27 分钟前
javascript之dom访问css
开发语言·javascript·css