写这个的时候居然没有看到原来CSDN已经有这个功能了,写完代码了突然发现原来早就已经有了。

现把代码贴出来吧,虽然有很多解析HTML的开源类库如:http://htmlagilitypack.codeplex.com/,但我一直习惯于正则匹配。

截图:

呵呵,起码还能看吧@——#

 private void button1_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(txtCsdnUrl.Text.Trim()))
{
string url = txtCsdnUrl.Text.Trim();
string htmlSource = string.Empty;
htmlSource = GetHtmlSource(url);
int pageCount = GetPageCount(htmlSource);
string context = string.Empty; if (pageCount > )
{
for (int i = ; i <= pageCount; i++)
{
htmlSource = GetHtmlSource(url + "?page=" + i); context+= GetLZArticle(htmlSource);
}
}
else
{
context += GetLZArticle(htmlSource);
} richTextBox1.Text = context; }
else
{
MessageBox.Show("请输入地址");
}
} /// <summary>
/// 获取源代码
/// </summary>
/// <param name="Url"></param>
/// <returns></returns>
public string GetHtmlSource(string Url)
{
WebClient client = new WebClient();
client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
Stream data = client.OpenRead(Url);
string result = string.Empty;
using (StreamReader reader = new StreamReader(data, Encoding.UTF8))
{
result = reader.ReadToEnd();
} return result;
} /// <summary>
/// 获取贴子总页数 URL格式:http://bbs.csdn.net/topics/390730011?page=2
/// </summary>
/// <returns>返回最大页数</returns>
public int GetPageCount(string HtmlSource)
{
int pageCount = ; Regex reg = new Regex("<select class=\"jumpMenu\" name=\"jumpMenu\">(?<val>.*?)</select>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
string htmlSource = HtmlSource;
Regex reg1 = new Regex("<option.*?>(?<val>.*?)</option>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
int count = reg1.Matches(reg.Match(htmlSource).Groups["val"].Value).Count; int.TryParse(reg1.Matches(reg.Match(htmlSource).Groups["val"].Value)[count - ].Groups["val"].Value,
out pageCount); return pageCount;
} /// <summary>
/// 获取文章标题
/// </summary>
/// <param name="HtmlSource">网页内容</param>
/// <returns></returns>
public string GetArticleTitle(string HtmlSource)
{
string title = string.Empty; Regex reg = new Regex("<span class=\"title text_overflow\">(?<title>.*?)</span>", RegexOptions.Singleline | RegexOptions.IgnoreCase); title = reg.Match(HtmlSource).Groups["title"].Value; return title;
} public string GetAuthorName(string HtmlSource)
{
string result = string.Empty; Regex regex = new Regex("<a class=\"p-author\" href=\"#\">(?<value>.*?)</a>"); result = regex.Match(HtmlSource).Groups["value"].Value; return result;
} public string GetLZArticle(string HtmlSource)
{ string result = string.Empty;
string authorName = GetAuthorName(HtmlSource); Regex regex = new Regex("<td valign=\"top\" class=\"post_info .*?\" data-username=\"" + authorName + "\".*?>.*?<div class=\"post_body\">(?<value>.*?)</div>.*?</td>", RegexOptions.Singleline | RegexOptions.IgnoreCase); for (int i = ; i < regex.Matches(HtmlSource).Count; i++)
{
result += regex.Matches(HtmlSource)[i].Groups["value"].Value;
result += "--------------------分隔线--------------------";
}
return result.Trim().Replace("<br />","\r\n");
}

代码都在这里了。

最新文章

  1. 【转】输入/输出流 - 深入理解Java中的流 (Stream)
  2. CentOS下MySQL数据库安装
  3. Atitit.mvc的趋势与未来attilax总结
  4. vm导入后远程桌面无法登陆域账户
  5. 二模 (8) day1
  6. 14个超赞的响应式HTML5模板免费下载
  7. UVA 10585 Center of symmetry
  8. mysql学习笔记5
  9. SQL Server调优系列基础篇 - 并行运算总结(二)
  10. 转:测试计划(出处:: 51Testing软件测试网--zfx081)
  11. Android官方技术文档翻译——Ant 任务
  12. win7程序关闭后弹出 程序兼容性助手 这个程序可能安装不正确 如果此程序没有正确安装,处理方式
  13. C# Programming Study #2
  14. Android基础知识-1
  15. mysql免安装版使用方法
  16. create schema 与create database的区别
  17. 解决相关css基础问题
  18. scrapy爬取小说盗墓笔记
  19. Oracle_CDC异步Autolog online redo部署示例
  20. C++设计模式——模板方法模式

热门文章

  1. java.lang.IllegalArgumentException: Document base F:\personal\projects\annoMVC\web does not exist or is not a readable directory
  2. 【extjs6学习笔记】1.10 初始: 定义类
  3. repair table
  4. meterpreter &gt; run post/windows/capture/keylog_recorder
  5. yii相关手册文档
  6. 如何用WebIDE打开并运行CRM Fiori应用
  7. 打包ios软件并发布到应用商店
  8. IOS 旋转+缩放(手势识别)
  9. IOS tableView的数据刷新
  10. 【洛谷】CYJian的水题大赛 解题报告