HtmlAgilityPack使用(一)【获取文档链接】
来源:互联网 发布:淘宝达人个人中心 编辑:程序博客网 时间:2024/06/11 05:19
GetDocLinks.cs代码:
using System;using System.Collections;namespace HtmlAgilityPack.Samples{ //获取文档链接 class GetDocLinks { [STAThread] static void Main(string[] args) { HtmlWeb hw = new HtmlWeb(); string url = @"http://www.microsoft.com"; HtmlDocument doc = hw.Load(url); doc.Save("mshome.htm"); DocumentWithLinks nwl = new DocumentWithLinks(doc); Console.WriteLine("链接 urls:"); for(int i=0;i<nwl.Links.Count;i++) { Console.WriteLine(nwl.Links[i]); } Console.WriteLine("引用 urls:"); for(int i=0;i<nwl.References.Count;i++) { Console.WriteLine(nwl.References[i]); } Console.ReadKey(); } } /// <summary> /// Represents a document that needs linked files to be rendered, such as images or css files, and points to other HTML documents. /// 表示需要呈现链接文件的文档,如图像或CSS文件,并指向其他HTML文档。 /// </summary> public class DocumentWithLinks { private ArrayList _links; private ArrayList _references; private HtmlDocument _doc; /// <summary> /// 创建一个DocumentWithLinkedFiles的实例。 /// </summary> /// <param name="doc">输入HTML文件。不可能为null。</param> public DocumentWithLinks(HtmlDocument doc) { if (doc == null) { throw new ArgumentNullException("doc"); } _doc = doc; GetLinks(); GetReferences(); } private void GetLinks() { _links = new ArrayList(); HtmlNodeCollection atts = _doc.DocumentNode.SelectNodes("//*[@background or @lowsrc or @src or @href]"); if (atts == null) return; foreach(HtmlNode n in atts) { ParseLink(n, "background"); ParseLink(n, "href"); ParseLink(n, "src"); ParseLink(n, "lowsrc"); } } private void GetReferences() { _references = new ArrayList(); HtmlNodeCollection hrefs = _doc.DocumentNode.SelectNodes("//a[@href]"); if (hrefs == null) return; foreach(HtmlNode href in hrefs) { _references.Add(href.Attributes["href"].Value); } } private void ParseLink(HtmlNode node, string name) { HtmlAttribute att = node.Attributes[name]; if (att == null) return; //如果name = href,我们只对<link>标签感兴趣 if ((name == "href") && (node.Name != "link")) return; _links.Add(att.Value); } /// <summary> /// 获取在HTML文档中声明的链接列表 /// </summary> public ArrayList Links { get { return _links; } } /// <summary> /// 获取其他HTML文档的引用链接列表,因为它们是在HTML文档中声明的。 /// </summary> public ArrayList References { get { return _references; } } }}
运行结果如图:
阅读全文
0 0
- HtmlAgilityPack使用(一)【获取文档链接】
- HtmlAgilityPack使用(一)【获取文档链接】
- HtmlAgilityPack官方文档(一)【Parser】
- .Net解析html文档使用类库HtmlAgilityPack
- HtmlAgilityPack 爬虫批量获取百度音乐下载地址一
- C# HTML解析工具HtmlAgilityPack使用实例(一)
- HtmlAgilityPack使用(二)【爬取每日一文】
- HtmlAgilityPack使用(二)【爬取每日一文】
- HtmlAgilityPack 总结(一)
- HtmlAgilityPack 总结(一)
- HtmlAgilityPack官方文档(二)【Selectors】
- HtmlAgilityPack官方文档(三)【Manipulation】
- HtmlAgilityPack官方文档(四)【Traversing】
- HtmlAgilityPack官方文档(五)【Writer】
- HtmlAgilityPack官方文档(六)【Utilities】
- HtmlAgilityPack官方文档(七)【Attributes】
- HtmlAgilityPack的简单使用
- 使用HtmlAgilityPack解析html
- [BZOJ]4455: [Zjoi2016]小星星 树形DP+容斥
- Centos升级Python 2.7并安装pip、ipython
- tecplot中的各种标注
- 机器人领域 期刊与会议
- SpringMVC+Spring+Hibernate+Oracle 实现图书管理(CRUD)
- HtmlAgilityPack使用(一)【获取文档链接】
- OOA/OOD/OOP(了解)
- Java(仿QQ/微信聊天)的时间格式方法
- js jquery 实现 排班,轮班,日历,日程。使用fullcalendar 插件
- 表格中的内容过长时不换行,以tips方式展示
- jQuery EasyUI(一)
- 任意多边形面积公式
- c/c++初始化动态列表、多态、动态分配、struct、引用
- PHP微信上传素材