加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – HTML敏捷包

发布时间:2020-12-15 17:39:29 所属栏目:百科 来源:网络整理
导读:我正在尝试使用 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非常相似;您可以在XmlDocument上使用MSDN进行广泛的概述;您可能还需要学习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
}

GetAttributeValue的第二个参数是在未找到该属性的情况下返回的默认值.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读