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. 代理支持

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

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

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

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

注意事项

  1. 法律合规

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

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

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

安装HtmlAgilityPack:

csharp 复制代码
dotnet add package HtmlAgilityPack

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

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

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

相关推荐
zwjapple2 小时前
docker-compose一键部署全栈项目。springboot后端,react前端
前端·spring boot·docker
像风一样自由20204 小时前
HTML与JavaScript:构建动态交互式Web页面的基石
前端·javascript·html
aiprtem5 小时前
基于Flutter的web登录设计
前端·flutter
浪裡遊5 小时前
React Hooks全面解析:从基础到高级的实用指南
开发语言·前端·javascript·react.js·node.js·ecmascript·php
why技术5 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
GISer_Jing5 小时前
0704-0706上海,又聚上了
前端·新浪微博
止观止6 小时前
深入探索 pnpm:高效磁盘利用与灵活的包管理解决方案
前端·pnpm·前端工程化·包管理器
whale fall6 小时前
npm install安装的node_modules是什么
前端·npm·node.js
烛阴6 小时前
简单入门Python装饰器
前端·python
袁煦丞6 小时前
数据库设计神器DrawDB:cpolar内网穿透实验室第595个成功挑战
前端·程序员·远程工作