[爬虫]通过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; }
}
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; }
}
相关文章推荐
- Diesel powered muscle can be consequence a whole lot of 6-pack abs
- iOS开发UI篇—UIScrollView控件实现图片缩放功能
- storm是如何保证at least once语义的
- 微信公众平台和微信开放平台的区别
- 相对路径 System.Web HttpServerUtilityBase Server.MapPath("~/")
- JS设计模式
- 该 App 的 Info.plist 不能包含阻碍在任何 iOS 设备上打开此 App 的 UIRequiredDeviceCapabilities 键值
- Linux下高性能网络编程中的几个TCP/IP选项_SO_REUSEADDR、SO_RECVBUF、SO_SNDBUF、SO_KEEPALIVE、SO_LINGER、TCP_CORK、TCP_NODE
- 测试
- Angular的一些知识点
- 同步机制摘要
- 创业项目招一起发展的java
- Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions
- php计算两个经纬度地点之间的距离(转)
- 网络上一些基本知识的理解
- 【Caffe】训练MNIST数据集模型
- iOS开发UI篇—UIScrollView控件介绍
- 2015 Multi-University Training Contest 2
- HDU 5308 I Wanna Become A 24-Point Master
- 字典树 hdu 1251