写这个的时候居然没有看到原来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");
}

代码都在这里了。

随手正则写的 CSDN【只看楼主】功能的更多相关文章

  1. 开发快很重要——如果只看法语或者产品结果C++似乎很强大,但是参与这个C++的开发过程,就会感觉到这种痛苦(Google也是这个看法)

    开发快很重要——如果只看语法或者产品结果C++似乎很强大,但是参与这个C++的开发过程,就会感觉到这种痛苦,太慢了,太麻烦了,虽然在反复调试和优化之后,最后产品的结果可能还不错. Delphi的最大特 ...

  2. css知识笔记:水平垂直居中(别只看,请实操!!!)

    css实现元素的水平垂直居中. (尝试采用5W2H方法说明): 别只看,请实操!!! What: 1.这篇文档主要描述元素水平方向居中的几种最常见和最实用的几种方式,并说明优缺点. 2.写这篇文章的目 ...

  3. css知识笔记:垂直居中(别只看,请实操!!!)

    css实现元素的垂直居中. (尝试采用5W2H方法说明): 别只看,请实操!!! What: 1.这篇文档主要描述元素水平方向居中的几种最常见和最实用的几种方式,并说明优缺点. 2.写这篇文章的目的, ...

  4. css知识笔记:水平居中(别只看,请实操!!!)

    css实现元素的水平居中. (尝试采用5W2H方法说明): 别只看,请实操!!! What: 1.这篇文档主要描述元素水平方向居中的几种最常见和最实用的几种方式,并说明优缺点. 2.写这篇文章的目的, ...

  5. C语言 不看怎么存,只看怎么读 ,短字节长字节之间的转换

    不看怎么存,只看怎么读 e.g. int a = 010; //8以8进制存 int b = 8;//8以10进制存 printf("%d %d\n",a,b); 以十进制取 a和 ...

  6. CSDN管理员看过来

    CSDN管理员看过来 你好.CSDN管理员,我想我被特殊对待了.我看了一些人的博客.终于发现仅仅有我博客的数据有异常.这算是给我的惊喜吗? 言归正传,我发现我博客上两个地方出现的文章的总数对不上.原创 ...

  7. 写给非专业人士看的 *** 简介(同时也解释了GFW )

    写给非专业人士看的 *** 简介 这个文章来源于一个朋友在***的过程中,搞不清楚 *** 的配置问题,在这里我想按照我对 *** 的理解简单梳理一下,以便一些非专业人士也能了解 long long ...

  8. windows server 2019 域控批量新增不用,只看这一篇就够了,别的不用看

    windows server 2019 域控批量新增不用,只看这一篇就够了,别的不用看 1. 新建excel表格 A B C D E 姓 名 全名 登录名 密码 李 四 李四 李四 test123!@ ...

  9. 成功的背后!(给所有IT人)----转载:来自CSDN第一名博主

    转载:来自CSDN第一名博主:http://blog.csdn.net/phphot/article/details/2187505 放在这里激励你我! 正文: 成功的背后,有着许多不为人知的故事,而 ...

随机推荐

  1. vue-elem-配置静态模拟数据访问接口

    使用本地mock数据模拟真实数据配置 static/data.json dev.server.js中 var app=express();之后添加以下代码, var appData=require(' ...

  2. It's the loneliest feeling not to know who you are.

    It's the loneliest feeling not to know who you are.最孤独的感觉莫过于不知道自己是谁.

  3. 报错:'byte' does not name a type

    这个错误是因为你在.cpp/.h中使用 byte 这个类型,把他修改成int就ok了

  4. centos7.3.1611安装及初始配置

    安装前规划: 主机名称 网络配置 分区配置 分区配置 自定义分区,标准分区 /boot 200M (可选) swap 内存1.5倍到2倍(不大于8G) / 根分区(100G到200G) 其余的备用(数 ...

  5. 本人常用的Phpstorm快捷键

    我设置的是eclipse的按键风格(按键习惯),不是phpstorm的风格 1.添加TODO(这个不是快捷键)://TODO 后面是说明,换行写实现代码 2.选择相同单词做一次性修改:Alt+J+鼠标 ...

  6. 详细讲解:通过phpstudy 设置站点域名、站点域名管理

    我们在本地编程的时候,会遇到路径特别长的情况,这样子我们在url中的输入就会变得不方便而且容易报错,那么在phpstudy这个环境中,有一个很好的功能,就是“站点域名管理”,他能让我们的url网址大大 ...

  7. [Git] Create a new repository on the command line

    echo "# xxx" >> README.md git init git add README.md git commit -m "first commi ...

  8. 在 Visual Studio 或 SQLServer Management Studio 的代码编辑器中使用正则表达式匹配日期格式

    使用正则查找时间格式文本 VS正则: (:z表示数字) ':z-:z-:z :z[\:]:z[\:]:z'

  9. myeclipse引入工程后运行出错

    An internal error occurred during: Launching efax on Tomcat 7.x . 项目运行时报错 因为你项目建的时候用的是Tomcat5.x 服务器 ...

  10. IOS 隐式动画(非Root Layer)

    ● 每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根 层) ● 所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动 ...