C# 如何从网站获取表的所有值

C# 如何从网站获取表的所有值,c#,html-agility-pack,C#,Html Agility Pack,我已尝试,但在字符串a中找到空值 好吧,这个让我困惑了一段时间,但我现在明白了。您可以从中仅获取表数据,而不是从中提取整个页面 尝试在iframe元素下选择#document节点的子元素时出现了一些奇怪的行为。有更多xpath经验的人可能能够解释这一点 现在,您可以使用以下xpath获取所有表行节点: string Url = "http://www.dsebd.org/latest_share_price_scroll_l.php"; HtmlWeb web = new HtmlWeb();

我已尝试,但在字符串a中找到空值

好吧,这个让我困惑了一段时间,但我现在明白了。您可以从中仅获取表数据,而不是从中提取整个页面

尝试在iframe元素下选择#document节点的子元素时出现了一些奇怪的行为。有更多xpath经验的人可能能够解释这一点

现在,您可以使用以下xpath获取所有表行节点:

string Url = "http://www.dsebd.org/latest_share_price_scroll_l.php";
HtmlWeb web = new HtmlWeb();

HtmlDocument doc = web.Load(Url);
string a = doc.DocumentNode.SelectNodes("//iframe*[@src=latest_share_price_all\"]//html/body/div/table/tbody")[0].InnerText;
这将为您提供所有表行节点。然后,您需要遍历刚刚获得的每个节点,并获得所需的值

例如,如果您希望获得交易代码、高交易量和交易量,您可以执行以下操作:

string url = "http://www.dsebd.org/latest_share_price_all.php";

HtmlDocument doc = new HtmlWeb().Load(url);
HtmlNode docNode = doc.DocumentNode;

var nodes = docNode.SelectNodes("//body/div/table/tr");
XPath使用基于1的索引,因此当您按数字引用表行中的特定单元格时,第一个元素位于索引1处,而不是像在C#数组中那样使用索引0

//Remove the first node because it is the header row at the top of the table
nodes.RemoveAt(0);
foreach(HtmlNode rowNode in nodes)
{
    HtmlNode tradingCodeNode = rowNode.SelectSingleNode("td[2]/a");
    string tradingCode = tradingCodeNode.InnerText;

    HtmlNode highNode = rowNode.SelectSingleNode("td[4]");
    string highValue = highNode.InnerText;

    HtmlNode volumeNode = rowNode.SelectSingleNode("td[11]");
    string volumeValue = volumeNode.InnerText;

    //Do whatever you want with the values here
    //Put them in a class or add them to a list
}