在winform窗体中建立一个RichTextBox,用来显示文章标题和链接地址
private void Form1_Load(object sender, EventArgs e)
{
//得到采集的全部文章
List<Article> list = Articles.GetArticles();
string TextStr = "";
//通过循环将全部文章遍历
if (list != null)
{
for (int i = 0; i < list.Count; i++)
{
Article temp = list[i];
//将数据输出显示
if (TextStr.Trim() != "")
{
TextStr += "\n";
}
TextStr += "标题:" + temp.Title;
TextStr += "\n链接:" + temp.Url;
TextStr += "\n阅读次数:" + temp.Views;
TextStr += "\n作者:" + temp.Author + "\n";
}
}
richTextBox1.Text = TextStr;
}
新建一个类来实现数据的采集,使用正则表达式进行采集!
/// <summary>
/// 文章信息操作类
/// </summary>
public class Articles
{
#region 采集数据并返回一个集合
/// <summary>
/// 采集博客园asp.net文章地址实例(上)
/// </summary>
/// <returns>一个装有全部采集数据的集合对象</returns>
public static List<Entity.Article> GetArticles()
{
//泛型集合,用来存入多篇文章信息
List<Entity.Article> list = new List<Entity.Article>();
#region 读取网页源代码并提取数据
//网页操作对象,我用来获取网页源码
HTML html = new HTML();
//对博客园每日排行数据进行采集
string htmlcode = html.GetHTML("http://www.cnblogs.com/cate/aspnet/","utf-8");
//提取博客园排行文章信息的正则表达式
//Regex regexarticles = new Regex(".+· <a\\s+id=\".+\" href=\"(?<url>.+)\"\\s+target=\"_blank\">(?<title>.+)</a> <span\\s+class=\".+\">\\(阅读:(?<views>\\d+)\\).*\\(评论:(?<reply>\\d+)\\).*\\((?<time>.+)\\)</span>\\s*</td>\\s*<td\\s+height=\"\\d+\">\\s+<a\\s+id=\".+\" href=\"(?<blog>.+)\">(?<author>.+)</a>");
//Regex regexarticles = new Regex("");
Regex regexarticles = new Regex("<div class=\"post_item_body\">\\s+<h3><a class=\"titlelnk\" href=\"(?<url>.+)\" target=\"_blank\">(?<title>.+)</a></h3>\\s+<p class=\"post_item_summary\">\\s+(?<ttt>.+)\\s+</p>\\s+<div class=\"post_item_foot\">\\s+<a href=\"(?<blog>.+)\" class=\"lightblue\">(?<author>.+)</a>\\s+发布于(?<time>.+)\r\\s+<span class=\"article_comment\"><a href=\"(?<url1>.+)\" title=\"(?<time>.+)\" class=\"gray\">\\s+评论((?<reply>.+))</a></span><span class=\"article_view\"><a href=\"(?<url>.+)\" class=\"gray\">阅读(?<views>.+)</a></span></div>\\s+</div>");
//所有匹配表达式的内容
MatchCollection marticles = regexarticles.Matches(htmlcode);
///遍历匹配内容
foreach (Match m in marticles)
{
Entity.Article test = new Entity.Article();
test.Category = "博客园asp.net文章"; //设置分类
test.Title = m.Groups["title"].Value.Trim(); //设置标题
test.Url = m.Groups["url"].Value.Trim(); //设置连接
test.Url1 = m.Groups["url1"].Value.Trim(); //设置连接
test.Views = int.Parse(m.Groups["views"].Value.Trim('(').Trim(')')); //设置浏览次数
test.Replys = int.Parse(m.Groups["reply"].Value.Trim('(').Trim(')')); //设置评论次数
test.Datatime = m.Groups["time"].Value.Trim(); //设置发布时间
test.Author = m.Groups["author"].Value.Trim(); //设置作者
test.Site = m.Groups["blog"].Value.Trim(); //设置文章出处
list.Add(test);
}
Regex regexarticles1 = new Regex("<div class=\"post_item_body\">\\s+<h3><a class=\"titlelnk\" href=\"(?<url>.+)\" target=\"_blank\">(?<title>.+)</a></h3>\\s+<p class=\"post_item_summary\">\\s+(?<ttt>.+)\\s+</p>\\s+<div class=\"post_item_foot\">\\s+<a href=\"(?<blog>.+)\" class=\"lightblue\">(?<author>.+)</a>\\s+发布于(?<time>.+)\r\\s+<span class=\"article_comment\"><a href=\"(?<url1>.+)\" title=\"\" class=\"gray\">\\s+评论((?<reply>.+))</a></span><span class=\"article_view\"><a href=\"(?<url>.+)\" class=\"gray\">阅读(?<views>.+)</a></span></div>\\s+</div>");
//所有匹配表达式的内容
MatchCollection marticles1 = regexarticles1.Matches(htmlcode);
///遍历匹配内容
foreach (Match m in marticles1)
{
Entity.Article test = new Entity.Article();
test.Category = "博客园asp.net文章"; //设置分类
test.Title = m.Groups["title"].Value.Trim(); //设置标题
test.Url = m.Groups["url"].Value.Trim(); //设置连接
test.Url1 = m.Groups["url1"].Value.Trim(); //设置阅读连接
test.Views = int.Parse(m.Groups["views"].Value.Trim('(').Trim(')')); //设置浏览次数
test.Replys = int.Parse(m.Groups["reply"].Value.Trim('(').Trim(')')); //设置评论次数
test.Datatime = m.Groups["time"].Value.Trim(); //设置发布时间
test.Author = m.Groups["author"].Value.Trim(); //设置作者
test.Site = m.Groups["blog"].Value.Trim(); //设置文章出处
list.Add(test);
}
#endregion
//因集合已经实例化,所以判断,无值则返回null
if (list.Count == 0)
return null;
else
return list;
}
#endregion
}
效果:
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。