要写一个使用C#和HttpClient的爬虫程序。首先,我需要了解HttpClient的基本用法。HttpClient是用来发送HTTP请求和接收响应的类,对吧?我记得在C#中使用它的时候需要注意一些事情,比如最好使用单例实例,而不是频繁创建和销毁,这样可以避免端口耗尽的问题。
以下是使用C#和HttpClient
编写的爬虫程序示例,包含详细注释和扩展说明:
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}");
}
}
}
HttpClientFactory
(ASP.NET Core环境)安装HtmlAgilityPack:
dotnet add package HtmlAgilityPack
注意:C# 7.1+ 支持异步Main方法,需在.csproj中添加: <PropertyGroup> <LangVersion>latest</LangVersion> </PropertyGroup>
这个爬虫框架可根据具体需求扩展更多功能,建议在实际使用中遵守目标网站的服务条款和相关法律法规。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。