HtmlAgilityPack使用(一)【获取文档链接】
2017-12-13 16:23
393 查看
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; } } } }
运行结果如图:
相关文章推荐
- HtmlAgilityPack使用(一)【获取文档链接】
- 爬虫技术(六)-- 使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)
- 爬虫技术 -- 进阶学习(九)使用HtmlAgilityPack获取页面链接(附c#代码及插件下载)
- .Net解析html文档使用类库HtmlAgilityPack
- .Net解析html文档类库HtmlAgilityPack完整使用说明
- 【C#】获取网页内容及HTML解析器HtmlAgilityPack的使用
- HtmlAgilityPack.dll的使用 获取HTMLid
- .Net解析html文档类库HtmlAgilityPack完整使用说明
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies
- 【.NET】使用HtmlAgilityPack抓取网页数据
- 使用HtmlAgilityPack实现对网页内容的抓取
- 使用HtmlAgilityPack解析Html(非常好用)
- HtmlAgilityPack.dll爬虫获取百度音乐批量下载地址(C#源码)
- 使用HtmlAgilityPack更好的HTML分析和验证
- 使用HtmlAgilityPack更好的HTML分析和验证
- 使用Python中的HTMLParser、cookielib抓取和解析网页、从HTML文档中提取链接、图像、文本、Cookies(二)
- [翻译]使用HtmlAgilityPack更好的HTML分析和验证
- C# 新闻采集(HtmlAgilityPack.dll)的使用
- Net处理html页面元素工具类(HtmlAgilityPack.dll)的使用
- HtmlAgilityPack.dll爬虫获取百度音乐批量下载地址(C#源码)