首页 > 技术文章 > [爬虫]通过url获取连接地址中的数据

taidou 2015-07-24 13:58 原文

1. 要想获取指定连接的数据,那么就得使用HtmlDocument对象,要想使用HtmlDocument对象就必需引用using HtmlAgilityPack;

2. 详细步骤如下:

    步骤一:

        获取链接地址内容:

        var html =HttpDownLoadHelper.GetUtf8Html("链接地址");

HttpDownLoadHelper类中的内容如下:

public class HttpDownLoadHelper

{

/// <summary>

/// 根据URL获取一个页面的Html内容

/// </summary>

/// <param name="url"></param>

/// <returns></returns>

public static string GetUtf8Html(string url)

{

WebClient wc = new WebClient();

wc.Encoding = Encoding.UTF8;

var html = wc.DownloadString(url);

return html;

}

}

    步骤二:

        判断获取到的内容是否为空?

    步骤三:

        获取数据:

            ·实例化"HtmlDocument HTML文档】"对象

                HtmlDocument doc = new HtmlDocument();

            ·载入获取到的内容

                doc.LoadHtml(html);

            ·获取文档中的根节点

                HtmlNode rootNode = doc.DocumentNode;

            ·从根节点中通过标签获取指定的内容。

    HtmlNodeCollection titleNodes = rootNode.SelectNodes("对应的标签");

        存储数据:

            ·创建一个存放数据的List集合

            List<NewsList> newsList=new List<NewsList>();

NewsList对象的代码如下:

            public class NewsList

        {

        public string Title { get; set; }

        public string Url { get; set; }

        }    

            ·将数据添加到集合中:

            foreach (var title in titleNodes)

{

NewsList news=new NewsList();

news.Title = title.GetAttributeValue("title", "");

                        // title是标签的属性

news.Url="http://www.yulinu.edu.cn"+title.GetAttributeValue("href", "");

                        //href是标签的属性。

newsList.Add(news);

}

 

具体事例:【获取榆林学院首页中的新闻列表】

·引用using HtmlAgilityPack;

HtmlAgilityPack.dll的下载地址:http://htmlagilitypack.codeplex.com/【里面有支持各种.NET Framework的版本的dll。】

·主方法:

public static void Main(string[] args)

{

//创建一个存放新闻的List集合    

List<NewsList> newsList=new List<NewsList>();

//根据url获取一个页面的Html内容。

var html = HttpDownLoadHelper.GetUtf8Html("http://www.yulinu.edu.cn/news.jsp?urltype=tree.TreeTempUrl&wbtreeid=1036");

//判断是否为空

if (!string.IsNullOrEmpty(html))

{

HtmlDocument doc = new HtmlDocument(); //实例化html实例对象

doc.LoadHtml(html);//载入html文档

HtmlNode rootNode = doc.DocumentNode; //获取文档中的根节点

//从根节点中通过标签获取指定的内容。

HtmlNodeCollection titleNodes = rootNode.SelectNodes("//div[@class='Classbox List']/ul/li/a");

foreach (var title in titleNodes)

{

NewsList news=new NewsList();

news.Title = title.GetAttributeValue("title", "");

news.Url = "http://www.yulinu.edu.cn" + title.GetAttributeValue("href", "");

newsList.Add(news);

}

}

//输出标题和地址

foreach (var list in newsList)

{

Console.WriteLine("新闻标题为:{0},新闻链接地址为:{1}",list.Title,list.Url);

}

Console.WriteLine("总共有{0}条新闻",newsList.Count);

Console.ReadKey();

}

·HttpDownLoadHelper代码如下:

    public class HttpDownLoadHelper

{

/// <summary>

/// 根据URL获取一个页面的Html内容

/// </summary>

/// <param name="url"></param>

/// <returns></returns>

public static string GetUtf8Html(string url)

{

WebClient wc = new WebClient();

wc.Encoding = Encoding.UTF8;

var html = wc.DownloadString(url);

return html;

}

}

·NewsList代码如下:

public class NewsList

{

public string Title { get; set; }

public string Url { get; set; }

}

推荐阅读