您的位置:首页 > 其它

[爬虫]通过url获取连接地址中的数据

2015-07-24 13:58 369 查看
1. 要想获取指定连接的数据,那么就得使用HtmlDocument对象,要想使用HtmlDocument对象就必需引用usingHtmlAgilityPack;

2. 详细步骤如下:


    步骤一:


        获取链接地址内容:


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

HttpDownLoadHelper类中的内容如下:


publicclassHttpDownLoadHelper

{


///
<summary>

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

///
</summary>

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

///
<returns></returns>

publicstaticstringGetUtf8Html(stringurl)


{


WebClientwc=newWebClient();


wc.Encoding=Encoding.UTF8;


varhtml=wc.DownloadString(url);


returnhtml;


}


}


    步骤二:


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


    步骤三:


        获取数据:


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


                HtmlDocumentdoc=newHtmlDocument();

            ·载入获取到的内容


                doc.LoadHtml(html);

            ·获取文档中的根节点


                HtmlNoderootNode=doc.DocumentNode;

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


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

        存储数据:


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


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

NewsList对象的代码如下:


            publicclassNewsList

        {


        publicstringTitle { get; set; }


        publicstringUrl { get; set; }


        }    


            ·将数据添加到集合中:


            foreach (vartitleintitleNodes)


{


NewsListnews=newNewsList();


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


                        // title是标签的属性

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


                        //href是标签的属性。


newsList.Add(news);


}


 

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


·引用usingHtmlAgilityPack;

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

·主方法:


publicstaticvoidMain(string[] args)


{


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

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


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

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


//判断是否为空

if (!string.IsNullOrEmpty(html))


{


HtmlDocumentdoc=newHtmlDocument(); //实例化html实例对象

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

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

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

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


foreach (vartitleintitleNodes)


{


NewsListnews=newNewsList();


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


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


newsList.Add(news);


}


}


//输出标题和地址

foreach (varlistinnewsList)


{


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


}


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


Console.ReadKey();


}


·HttpDownLoadHelper代码如下:


    publicclassHttpDownLoadHelper

{


///
<summary>

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

///
</summary>

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

///
<returns></returns>

publicstaticstringGetUtf8Html(stringurl)


{


WebClientwc=newWebClient();


wc.Encoding=Encoding.UTF8;


varhtml=wc.DownloadString(url);


returnhtml;


}


}


·NewsList代码如下:


publicclassNewsList

{


publicstringTitle { get; set; }


publicstringUrl { get; set; }


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