class="brush:csharp;gutter:true;">public class HttpCnblogs { public static List<CnblogsModel> HttpGetHtml() { HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.cnblogs.com/"); request.Method = "GET"; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.UserAgent = " Mozilla/5.0 (Windows NT 6.1; WOW64; rv:28.0) Gecko/20100101 Firefox/28.0"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader sr = new StreamReader(stream); string articleContent = sr.ReadToEnd(); List<CnblogsModel> list = new List<CnblogsModel>(); #region 正则表达式 //div post_item_body列表 Regex regBody = new Regex(@"<div\sclass=""post_item_body"">([\s\S].*?)</div>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); //a标签 文章标题 作者名字 评论 阅读 Regex regA = new Regex("<a[^>]*?>(.*?)</a>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); //p标签 文章内容 Regex regP = new Regex(@"<p\sclass=""post_item_summary"">(.*?)</p>", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); //提取评论 阅读次数如:评论(10)-》10 Regex regNumbernew = new Regex(@"\d+", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); //提取时间 Regex regTime = new Regex(@"\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); #endregion MatchCollection mList = regBody.Matches(articleContent); CnblogsModel model = null; String strBody = String.Empty; for (int i = 0; i < mList.Count; i++) { model = new CnblogsModel(); strBody = mList[i].Groups[1].ToString(); MatchCollection aList = regA.Matches(strBody); int aCount = aList.Count; model.ArticleTitle = aList[0].Groups[1].ToString(); model.ArticleAutor = aCount == 5 ? aList[2].Groups[1].ToString() : aList[1].Groups[1].ToString(); model.ArticleComment = Convert.ToInt32(regNumbernew.Match(aList[aCount-2].Groups[1].ToString()).Value); model.ArticleTime = regTime.Match(strBody).Value; model.ArticleView = Convert.ToInt32(regNumbernew.Match(aList[aCount-1].Groups[1].ToString()).Value); model.ArticleContent = regP.Matches(strBody)[0].Groups[1].ToString(); list.Add(model); } return list; } } public class CnblogsModel { /// <summary> /// 文章标题 /// </summary> public String ArticleTitle { get; set; } /// <summary> /// 文章内容摘要 /// </summary> public String ArticleContent { get; set; } /// <summary> /// 文章作者 /// </summary> public String ArticleAutor { get; set; } /// <summary> /// 文章发布时间 /// </summary> public String ArticleTime { get; set; } /// <summary> /// 文章评论量 /// </summary> public Int32 ArticleComment { get; set; } /// <summary> /// 文章浏览量 /// </summary> public Int32 ArticleView { get; set; } }
写的不好,还请见谅,准备下面试去。。