我已经对此进行了很多搜索,但它就是行不通。我想获取元标记的内容属性中的值。这是我的代码:
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();
}
这不会返回任何内容。任何帮助将不胜感激。
最佳答案
您的查询选择器行中似乎缺少右括号字符。只需如下一行简单的代码,您就可以完成所需的操作。
doc.DocumentNode.SelectSingleNode("//meta[@property='og:image']").GetAttributeValue("content", null);
关于c# - htmlagilitypack 获取具有内容属性的元标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41454453/