使用htmlagilitypack从表中读取不可见数据 [英] Read invisible data from table with htmlagilitypack
本文介绍了使用htmlagilitypack从表中读取不可见数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,
我有这个html与表。
我可以得到col1和cal2但我不知道如何得到也值data-index,data-name:
< table class =footable tableid =footable>
< tbody>
< tr class =trclass reddata-index =123data-name =Apple>
< td class =col1> Green< / td>
< td class =col2> 1.25< / td>
< / td>< / tr>
< / tbody>
< / table>
任何帮助?
我尝试过的事情:
// @Nuget:HtmlAgilityPack
使用System;
使用System.Xml;
使用System.IO;
使用HtmlAgilityPack;
公共课程
{
public static void Main()
{
var html =
@< html>
< tbody>
< table id = \'footable \'>
< tr class = \'trclass red \'data-index = \'123 \\ \\'data-name = \'Apple \'>
< td class = \'col1 \'>绿色< / td>
< td class = \' col2 \'> 1.25< / td>< / tr>
< / table>
< / tbody>< / html>;
var htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(html);
var tbody = htmlDoc.DocumentNode.SelectNodes(// table [contains(@id,'foo')] // tr // td);
foreach(var nob in tbody)
{
Console.Write(nob.InnerHtml);
}
}
}
解决方案
在节点上使用.Attributes来读取数据索引等节点属性
选择节点| Html敏捷包 [ ^ ]
为什么不把它读成XML,因为它是?
Hello,
I have this html with table.
I can get "col1" and "cal2" but I don't know how to get also value of "data-index", "data-name":
<table class="footable table" id="footable"> <tbody> <tr class="trclass red" data-index="123" data-name="Apple"> <td class="col1" >Green</td> <td class="col2" >1.25</td> </td></tr> </tbody> </table>
Any help?
What I have tried:
// @Nuget: HtmlAgilityPack using System; using System.Xml; using System.IO; using HtmlAgilityPack; public class Program { public static void Main() { var html = @"<html> <tbody> <table id=\'footable\'> <tr class=\'trclass red\' data-index=\'123\' data-name=\'Apple\'> <td class=\'col1\' >Green</td> <td class=\'col2\' > 1.25</td></tr> </table> </tbody></html>"; var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); var tbody = htmlDoc.DocumentNode.SelectNodes("//table[contains(@id, 'foo')]//tr//td"); foreach(var nob in tbody) { Console.Write(nob.InnerHtml); } } }
解决方案
Use .Attributes on the node to read that nodes attributes like data-index etc
Select Nodes | Html Agility Pack[^]
Why not just read it as XML, given that it is?
这篇关于使用htmlagilitypack从表中读取不可见数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文