c#代码爬取网页中某行数据
时间: 2024-04-20 22:18:47 浏览: 39
以下是使用C#代码爬取网页中某行数据的示例:
```csharp
using System;
using System.Net;
class Program
{
static void Main(string[] args)
{
// 创建WebClient对象
WebClient client = new WebClient();
// 下载网页HTML
string html = client.DownloadString("https://www.example.com");
// 解析HTML
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
// 获取某行数据
HtmlAgilityPack.HtmlNode rowNode = doc.DocumentNode.SelectSingleNode("//tr[@class='row']");
string rowData = rowNode.InnerText;
// 输出结果
Console.WriteLine(rowData);
}
}
```
这段代码使用了`WebClient`类来下载网页的HTML内容,并使用`HtmlAgilityPack`库来解析HTML。通过`SelectSingleNode`方法可以选择指定的HTML节点,然后使用`InnerText`属性获取该节点的文本内容。