<...">

Get the value of the HtmlAgilityPack attribute

I want to get the value of the HtmlAgilityPack attribute. HTML code:

<link href="style.css">
<link href="anotherstyle.css">
<link href="anotherstyle2.css">
<link itemprop="thumbnailUrl" href="http://image.jpg">
<link href="anotherstyle5.css">
<link href="anotherstyle7.css">

I want to get the latest href attribute.

My C # code:

HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldoc = web.Load(Url);
htmldoc.OptionFixNestedTags = true;
var navigator = (HtmlNodeNavigator)htmldoc.CreateNavigator();
string xpath = "//link/@href";
string val = navigator.SelectSingleNode(xpath).Value;

But this code returns the first href value.

+11
source share
5 answers

After XPath selects linkelements that have an attribute href. Then from the links you select the last one:

var link = doc.DocumentNode.SelectNodes("//link[@href]").LastOrDefault();
// you can also check if link is not null
var href = link.Attributes["href"].Value; // "anotherstyle7.css"

You can also use the last()XPath operator

var link = doc.DocumentNode.SelectSingleNode("/link[@href][last()]");
var href = link.Attributes["href"].Value;

UPDATE: if you want to get the last element with attributes itempropand href, use XPath //link[@href and @itemprop][last()]or //link[@href and @itemprop]if you go with the first approach.

+17
source

you need something like this:

HtmlWeb web = new HtmlWeb();
HtmlAgilityPack.HtmlDocument htmldoc = web.Load(Url);
htmldoc.OptionFixNestedTags = true;
var navigator = (HtmlNodeNavigator)htmldoc.CreateNavigator();
string xpath = "//link[@itemprop]/@href";
string val = navigator.SelectSingleNode(xpath).Value;
+3

- Htmldocument .

        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(Url);
        var output = doc.DocumentNode.SelectNodes("//link[@href]").LastOrDefault();
        var data = output.Attributes["href"].Value;

or load the webpage as an Htmldocument and get a collection of all the selected link tags, then move using a loop, then access the last attribute of the select tag.

        HtmlWeb web = new HtmlWeb();
        HtmlDocument doc = web.Load(Url);
        int count = 0;
        string data = "";
        var output = doc.DocumentNode.SelectNodes("//link[@href]");

        foreach (var item in output)
        {
            count++;
            if (count == output.Count)
            {
                data=item.Attributes["href"].Value;
                break;
            }
        }
+1
source

Ok, I came to the following:

var link = htmldoc.DocumentNode.SelectSingleNode("//link[@itemprop='thumbnailUrl']");
var href = link.Attributes["href"].Value;
0
source

Get HtmlNode by attribute value:

public static class Extensions
{
    public static HtmlNode GetNodeByAttributeValue(this HtmlNode htmlNode, string attributeName, string attributeValue)
    {
        if (htmlNode.Attributes.Contains(attributeName))
        {
            if (string.Compare(htmlNode.Attributes[attributeName].Value, attributeValue, true) == 0)
            {
                return htmlNode;
            }
        }

        foreach (var childHtmlNode in htmlNode.ChildNodes)
        {
            var resultNode = GetNodeByAttributeValue(childHtmlNode, attributeName, attributeValue);
            if (resultNode != null) return resultNode;
        }

        return null;
    }
}

Usage

Usage
var searchResultsDiv = pageDocument.DocumentNode.GetNodeByAttributeValue("someattributename", "resultsofsearch");
0
source

All Articles