📄 articles.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
namespace Plug.Article
{
/// <summary>
/// 文章信息操作类
/// </summary>
public class Articles
{
#region 采集数据并返回一个集合
/// <summary>
/// 采集博客园热门文章
/// </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/TopPosts.aspx","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>");
//所有匹配表达式的内容
MatchCollection marticles = regexarticles.Matches(htmlcode);
///遍历匹配内容
foreach (Match m in marticles)
{
Entity.Article test = new Entity.Article();
test.Category = "博客园热门文章"; //设置分类
test.Title = m.Groups["title"].Value; //设置标题
test.Url = m.Groups["url"].Value; //设置连接
test.Views = int.Parse(m.Groups["views"].Value); //设置浏览次数
test.Replys = int.Parse(m.Groups["reply"].Value); //设置评论次数
test.Datatime = m.Groups["time"].Value; //设置发布时间
test.Author = m.Groups["author"].Value; //设置作者
test.Site = m.Groups["blog"].Value; //设置文章出处
list.Add(test);
}
#endregion
//因集合已经实例化,所以判断,无值则返回null
if (list.Count == 0)
return null;
else
return list;
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -