-2

只是想弄清楚如何从已经解析的信息中解析信息。

foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div [@class=\"result-link\"]"))
{
    if (node == null)
        Console.WriteLine("debug");
    else
    {
        //string h_url = node.Attributes["a"].Value;
        Console.WriteLine(node.InnerHtml);
    }
}

所以你可以看看我想用'string h_url'声明做什么。在“结果链接”div 类中有一个 href 属性,我试图获取 href 值。所以链接基本上。

似乎无法弄清楚。我尝试使用 Attributes 数组:

string h_url = node.Attributes["//a[@href].Value;

没有运气。

4

1 回答 1

2

您可以使用 XPath 选择相对于当前节点的元素:

HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//div[@class='result-link']");
if (nodes != null)
{
    foreach (HtmlNode node in nodes)
    {
        HtmlNode a = node.SelectSingleNode("a[@href]");
        if (a != null)
        {
            // use  a.Attributes["href"];
        }

        // etc...
    }
}
于 2014-12-13T17:57:00.127 回答