[爬虫]通过url获取连接地址中的数据
1. 要想獲取指定連接的數(shù)據(jù),那么就得使用HtmlDocument對(duì)象,要想使用HtmlDocument對(duì)象就必需引用using HtmlAgilityPack;
2. 詳細(xì)步驟如下:
????步驟一:
????????獲取鏈接地址內(nèi)容:
????????var html =HttpDownLoadHelper.GetUtf8Html("鏈接地址");
HttpDownLoadHelper類中的內(nèi)容如下:
public class HttpDownLoadHelper
{
/// <summary>
/// 根據(jù)URL獲取一個(gè)頁(yè)面的Html內(nèi)容
/// </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;
}
}
????步驟二:
????????判斷獲取到的內(nèi)容是否為空?
????步驟三:
????????獲取數(shù)據(jù):
????????????·實(shí)例化"HtmlDocument 【HTML文檔】"對(duì)象
????????????????HtmlDocument doc = new HtmlDocument();
????????????·載入獲取到的內(nèi)容
????????????????doc.LoadHtml(html);
????????????·獲取文檔中的根節(jié)點(diǎn)
????????????????HtmlNode rootNode = doc.DocumentNode;
????????????·從根節(jié)點(diǎn)中通過(guò)標(biāo)簽獲取指定的內(nèi)容。
????HtmlNodeCollection titleNodes = rootNode.SelectNodes("對(duì)應(yīng)的標(biāo)簽");
????????存儲(chǔ)數(shù)據(jù):
????????????·創(chuàng)建一個(gè)存放數(shù)據(jù)的List集合
????????????List<NewsList> newsList=new List<NewsList>();
NewsList對(duì)象的代碼如下:
????????????public class NewsList
????????{
????????public string Title { get; set; }
????????public string Url { get; set; }
????????}????
????????????·將數(shù)據(jù)添加到集合中:
????????????foreach (var title in titleNodes)
{
NewsList news=new NewsList();
news.Title = title.GetAttributeValue("title", "");
????????????????????????// title是標(biāo)簽的屬性
news.Url="http://www.yulinu.edu.cn"+title.GetAttributeValue("href", "");
????????????????????????//href是標(biāo)簽的屬性。
newsList.Add(news);
}
?
具體事例:【獲取榆林學(xué)院首頁(yè)中的新聞列表】
·引用using HtmlAgilityPack;
HtmlAgilityPack.dll的下載地址:http://htmlagilitypack.codeplex.com/【里面有支持各種.NET Framework的版本的dll。】
·主方法:
public static void Main(string[] args)
{
//創(chuàng)建一個(gè)存放新聞的List集合????
List<NewsList> newsList=new List<NewsList>();
//根據(jù)url獲取一個(gè)頁(yè)面的Html內(nèi)容。
var html = HttpDownLoadHelper.GetUtf8Html("http://www.yulinu.edu.cn/news.jsp?urltype=tree.TreeTempUrl&wbtreeid=1036");
//判斷是否為空
if (!string.IsNullOrEmpty(html))
{
HtmlDocument doc = new HtmlDocument(); //實(shí)例化html實(shí)例對(duì)象
doc.LoadHtml(html);//載入html文檔
HtmlNode rootNode = doc.DocumentNode; //獲取文檔中的根節(jié)點(diǎn)
//從根節(jié)點(diǎn)中通過(guò)標(biāo)簽獲取指定的內(nèi)容。
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);
}
}
//輸出標(biāo)題和地址
foreach (var list in newsList)
{
Console.WriteLine("新聞標(biāo)題為:{0},新聞鏈接地址為:{1}",list.Title,list.Url);
}
Console.WriteLine("總共有{0}條新聞",newsList.Count);
Console.ReadKey();
}
·HttpDownLoadHelper代碼如下:
????public class HttpDownLoadHelper
{
/// <summary>
/// 根據(jù)URL獲取一個(gè)頁(yè)面的Html內(nèi)容
/// </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; }
}
轉(zhuǎn)載于:https://www.cnblogs.com/taidou/p/4673306.html
總結(jié)
以上是生活随笔為你收集整理的[爬虫]通过url获取连接地址中的数据的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Atomikos 中文说明文档【转】
- 下一篇: LeetCode Letter Comb