您的位置:首页 > Web前端 > HTML

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;
}
}
}
}


运行结果如图:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: