1

如何从 html 文档中解析带有 URL 的 rel="canonical" 标记?

我想在这里找到网址:

<link rel="canonical" href="http://stackoverflow.com/questions/2593147/html-agility-pack-make-code-look-neat" />
4

3 回答 3

4

假设doc是你的HtmlDocument对象。

HtmlNodeCollection links = doc.DocumentNode.SelectNodes("//link[@rel]");

应该为您link提供具有rel属性的元素。现在迭代:

foreach (HtmlNode link in links)
{
    string url;
    if (link.Attributes["rel"] == "canonical") {
        url = link.Attributes["href"];
    }
}

此外,可以过滤 SelectNodes 调用中的链接,以仅获取具有“规范”的链接:doc.DocumentNode.SelectNodes("//link[@rel='canonical']");

未经测试的代码,但你明白了:)

于 2012-11-19T12:44:51.630 回答
3

接受的答案不再正确,更新的代码如下:

var links = htmlDoc.DocumentNode.SelectNodes("//link[@rel]");

string canonical;

foreach (HtmlNode link in links)
{
    if (link.Attributes["rel"].Value == "canonical")
    {
        canonical = link.Attributes["href"].Value;
    }
}
于 2016-02-07T18:49:11.340 回答
0
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(_html);

String link = (from x in doc.DocumentNode.Descendants()
           where x.Name == "link"
           && x.Attributes["rel"] != null
           && x.Attributes["rel"].Value == "canonical"
           && x.Attributes["href"] != null
           select x.Attributes["href"].Value).FirstOrDefault();
于 2012-11-19T12:45:40.077 回答