gpt4 book ai didi

c# - 在 C# 中使用 HtmlAgilityPack 解析 html

转载 作者:太空宇宙 更新时间:2023-11-03 23:28:21 26 4
gpt4 key购买 nike

WebClient webClient = new WebClient();
string page = webClient.DownloadString(
"http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);

我想解析上面给出的页面,但我想获取表的行信息。我试过几个例子,但我做不到。任何建议

最佳答案

例如,您可以像这样解析行:

using System.Net;
using HtmlAgilityPack;

namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
string page = webClient.DownloadString("http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(page);

HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
foreach (var cell in table.SelectNodes("tr/td"))
{
string someVariable = cell.InnerText;
}
}
}
}

为了完整起见,使用 LINQ 您可以轻松创建一个包含所有非空行值的枚举:

    private static void Main(string[] args)
{
WebClient webClient = new WebClient();
string page = webClient.DownloadString("http://www.deu.edu.tr/DEUWeb/Guncel/v2_index_cron.html");

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(page);

HtmlNode table = doc.DocumentNode.SelectSingleNode("//table");
var rows = table.SelectNodes("tr/td").Select(cell => cell.InnerText).Where(someVariable => !String.IsNullOrWhiteSpace(someVariable)).ToList();
}

关于c# - 在 C# 中使用 HtmlAgilityPack 解析 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33081673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com