我正在尝试使用HTML Agility Pack从以下位置获取描述文本:
<meta name="description" content="**this is the text i want to extract and store in a string**" />
有人在Stackoverflow上建议我使用HTMLAgilityPack。但是我不知道如何使用它,而且我找到的文档(包括下载文件中的文档)都有无效链接,因此无法查看文档。
请问有人可以帮我解决这个问题吗?
我正在尝试使用HTML Agility Pack从以下位置获取描述文本:
<meta name="description" content="**this is the text i want to extract and store in a string**" />
有人在Stackoverflow上建议我使用HTMLAgilityPack。但是我不知道如何使用它,而且我找到的文档(包括下载文件中的文档)都有无效链接,因此无法查看文档。
请问有人可以帮我解决这个问题吗?
用法与 XmlDocument
非常相似;您可以使用 MSDN 上的 XmlDocument
进行广泛概述;您可能还想了解 xpath 语法 (MSDN)。
示例:
HtmlDocument doc = new HtmlDocument();
doc.Load(path); // or .LoadHtml(html);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//meta[@name='description']");
if (node != null) {
string desc = node.GetAttributeValue("content", "");
// TODO: write desc somewhere
}
< p > GetAttributeValue
的第二个参数是在未找到属性时返回的默认值。 node.GetAttributeValue("content", "");
中的第二个字符串参数(空字符串)有什么用? - Alexpublic string HtmlAgi(string url, string key) {
公共字符串 HtmlAgi(字符串网址,字符串密钥) { var Webget = new HtmlWeb();
var doc = Webget.Load(url);
HtmlNode ourNode = doc.DocumentNode.SelectSingleNode(string.Format("//meta[@name='{0}']", key));
if (ourNode != null)
{
return ourNode.GetAttributeValue("content", "");
}
else
{
return "not fount";
}
}