gpt4 book ai didi

c# - 使用 HtmlAgilitypack 问题解析表

转载 作者:行者123 更新时间:2023-11-28 03:15:29 25 4
gpt4 key购买 nike

我正在尝试解析如下所示的表格:

<table><tbody>
<tr><th a href=""></th><th></th></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a"> </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="ttt"></table></td></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a"> </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="eee"></table></td></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a"> </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="rtr"></table></td></tr>
<tr><th a href=""></th><th></th></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a"> </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="ouu"></table></td></tr>
<tr><td class="v"></td><td class="d"></td><td class="h"></td><td class="a"> </td><td class="o"></td><td class="o"></td><td class="o"></td><td class="p"><table class="p" title="teee"></table></td></tr>
</tbody></table>

我在 ASP.net 中使用这段代码来获取我想要的每一行中的单元格:

var getHtmlWeb = new HtmlWeb();
var document = getHtmlWeb.Load(txtbox.Text);
//get tables
foreach (HtmlNode table in document.DocumentNode.SelectNodes("//table"))
{
//get each table row
foreach (HtmlNode row in table.SelectNodes("tr"))
{

Outputlabel.Text += "row: <br />";
//get table head tags that have a link, get the Inner text
if((row.SelectSingleNode("//th//a").InnerText) != null)
{

Outputlabel.Text += row.SelectSingleNode("//th//a").InnerText + "<br />";
}
// get the cells with the classes I want
string d = row.SelectSingleNode("//td[@class='d']").InnerText;
Outputlabel.Text += row.SelectSingleNode("//td[@class='d']").InnerText + " ";

string h = row.SelectSingleNode("//td[@class='h']").InnerText;
Outputlabel.Text += row.SelectSingleNode("//td[@class='h']").InnerText + " ";
string a = row.SelectSingleNode("//td[@class='a']").InnerText;
Outputlabel.Text += row.SelectSingleNode("//td[@class='a']").InnerText + " ";
string op = "";
//there are 3 classes in each row to have the class="o"
if (row.SelectNodes("//td[@class='o']") != null)
{
foreach (HtmlNode o in row.SelectNodes("//td[@class='o']"))
{
op += o.InnerText;
}
Outputlabel.Text += op + " ";
}

var pr = row.SelectSingleNode("//td//table[@class='p']");
string pr = probability.Attributes["title"].Value;

Outputlabel.Text += pr + "<br />";
}
}

我只得到第一个表的第一行,它重复了很多次......而且我没有得到类“o”和类“p”的表的标题在类“p"

最佳答案

        public static void Main(string[] args)
{
string html = @"<!DOCTYPE html>
<html>
<body>


<table>
<tbody>
<tr>
<th a href="""">a</th>
<th>d2</th>
</tr>
<tr>
<td class=""v"">b</td>
<td class=""d"">v</td>
<td class=""h"">g</td>
<td class=""a""> d </td>
<td class=""o"">e</td>
<td class=""o"">j</td>
<td class=""o"">z</td>
<td class=""p"">
<table class=""p"" title=""ttt""></table>
</td>
</tr>
<tr>
<td class=""v"">1</td>
<td class=""d"">2</td>
<td class=""h"">3</td>
<td class=""a""> 4 </td>
<td class=""o"">5</td>
<td class=""o"">6</td>
<td class=""o"">7</td>
<td class=""p"">
<table class=""p"" title=""eee""></table>
</td>
</tr>
<tr>
<td class=""v"">a1</td>
<td class=""d"">b2</td>
<td class=""h"">v3</td>
<td class=""a""> d4 </td>
<td class=""o"">e5</td>
<td class=""o"">j6</td>
<td class=""o"">z7</td>
<td class=""p"">
<table class=""p"" title=""rtr""></table>
</td>
</tr>
<tr>
<th a href="""">23</th>
<th>qaaa</th>
</tr>
<tr>
<td class=""v"">az</td>
<td class=""d"">b2</td>
<td class=""h"">a b</td>
<td class=""a""> aaaa </td>
<td class=""o"">aa</td>
<td class=""o"">a</td>
<td class=""o"">aaa</td>
<td class=""p"">
<table class=""p"" title=""ouu""></table>
</td>
</tr>
<tr>
<td class=""v"">z</td>
<td class=""d"">zz</td>
<td class=""h"">zzz</td>
<td class=""a""> zzzz </td>
<td class=""o"">zzzzzz</td>
<td class=""o"">zzzzz</td>
<td class=""o"">zzzzzzz</td>
<td class=""p"">
<table class=""p"" title=""teee""></table>
</td>
</tr>
</tbody>
</table></body>
</html>";

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


var coll = doc.DocumentNode.Descendants().Where(x => x.Name == "th" ||
(x.Name == "td" &&
(x.Attributes["class"].Value == "v" ||
x.Attributes["class"].Value == "d" ||
x.Attributes["class"].Value == "h" ||
x.Attributes["class"].Value == "a" ||
x.Attributes["class"].Value == "o")) ||
(x.Name=="table" && x.Attributes["class"] != null && x.Attributes["class"].Value == "p")).ToList();

foreach (HtmlNode node in coll)
{
if(node.Name=="table")
{
Console.WriteLine(node.Attributes["title"].Value);
}
else
{
Console.WriteLine(node.InnerText.Trim());
}
}
}

您应该采用特定元素而不是整个表格。下面是获取所有 th,td InnerText 并返回带有 class="p"table 标题的代码。

以后也请格式化您的 HTML!

这是您逻辑的关键部分。我没有选择 td class="p",因为您只从该元素中获取表格标题

var coll = doc.DocumentNode.Descendants().Where(x => x.Name == "th" || 
(x.Name == "td" &&
(x.Attributes["class"].Value == "v" ||
x.Attributes["class"].Value == "d" ||
x.Attributes["class"].Value == "h" ||
x.Attributes["class"].Value == "a" ||
x.Attributes["class"].Value == "o")) ||
(x.Name=="table" && x.Attributes["class"] != null && x.Attributes["class"].Value == "p")).ToList();

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

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