简体   繁体   中英

htmlagilitypack getting meta tag with content attributes

I've searched a lot about this but it just doesn't work. I want to get the value in content attribute of the meta tag. This is my code:

public string getTheImagesUrl(string url)
{
    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(url);

    string xpath = "//meta[@property='og:image'";
    StringBuilder sb = new StringBuilder();
    foreach (HtmlNode node in doc.DocumentNode.Descendants(xpath))
    {
        sb.AppendLine(node.Attributes["content"].Value);
    }
    return sb.ToString();
}

This returns nothing. Any help would be appreciated.

I could not get it to work with HtmlAgilityPack, but here is a working example with AngleSharp

var config = Configuration.Default.WithDefaultLoader();
string address = "http://stackoverflow.com";

return (await BrowsingContext.New(config).OpenAsync(address))
    .DocumentElement.Descendents()
    .Where(x => x.NodeType == NodeType.Element)
    .OfType<IHtmlMetaElement>()
    .Where(x => x.Attributes["property"]?.Value == "og:image")
    .Select(x => x.Attributes["content"]?.Value)
    .FirstOrDefault();

It seems you are missing the closing bracket char in your query selector line. You can do what you need with just a simple line of code as below.

doc.DocumentNode.SelectSingleNode("//meta[@property='og:image']").GetAttributeValue("content", null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM