天涯社区离线阅读器(实现只看楼主功能)
长期逛天涯社区,许多的经典的帖子都有许多页,而且里面还夹杂了大量的回复或者广告,不能像MOP一样只看楼主,这样阅读起来非常不便,故此写了一个小程序实现离线阅读并只看楼主功能。实现的思路是通过HttpWebRequest取得要查看的网页内容,然后用正则表达式将其中感性趣的部分取出来,我主要取了分页信息,同时分离出了各贴以及作者,这样可以实现查看任意一个人帖子的功能,如果需要的话可以将取出的数据保存在本地硬盘中以方便查看,在这个程序没有作本地保存。程序中的主要代码部分有:
1、从指定网址下载网页
public string Get_SourceHtml(string a_strUrl)
{

string strResult;
try
{
HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(a_strUrl);
myReq.Timeout = 60000;
HttpWebResponse HttpWResp = (HttpWebResponse)myReq.GetResponse();
Stream myStream = HttpWResp.GetResponseStream();
StreamReader sr = new StreamReader(myStream, Encoding.Default);
strResult = sr.ReadToEnd();
myStream.Close();
}
catch (Exception exp)
{
strResult = "错误:" + exp.Message;
}
return strResult;
}
2、分离出帖子正文
public string SplitHtml(string htmlContent)
{
string result =htmlContent;

//剪切掉正文后面的内容
Regex regexObj = new Regex("</div><center>");

Match m = regexObj.Match(result);

if (m.Value != string.Empty)
{
result = result.Substring(0, result.IndexOf(m.Value));
}
//清除掉广告
regexObj =new Regex(@"<center><IFRAME(.|\s)+?</IFRAME></center>");
result=regexObj.Replace(result,"");

//剪切掉正文前面的内容
//regexObj = new Regex("<TABLE align=center border=0 cellSpacing=0 width='100%'>");
regexObj = new Regex(@"<span id='AddMyDigest'></span>");

m = regexObj.Match(result);

if (m.Value != string.Empty)
{
result = result.Substring(result.IndexOf(m.Value));
}

return result;
}
3、获取指定作者的发帖在本地生成新的Html文件以便在程序内嵌浏览器中查看
public string GetAuthorsContent(string SourceHtml, string AuthorName)
{
StringBuilder html = new StringBuilder();

html.Append("<Html><body>");

SourceHtml = SplitHtml(SourceHtml);

Regex r = new Regex(@"<TABLE(.|\s)+?</table>");
MatchCollection ms = r.Matches(SourceHtml);

for (int i = 0; i < ms.Count - 2; i++)
{
Match m = ms[i];
int start = SourceHtml.IndexOf(m.Value) + m.Value.Length;

string title = m.Value;
Regex re = new Regex(@"k>(.|\s)+?<");
Match m1 = re.Match(title);

string author = m1.Value;
if (author.Length > 0)
author = author.Substring(2, author.IndexOf("<") - 2);
if (author == AuthorName)
{
html.Append(title);

Match NextMatch = ms[i + 1];
string content;
if (NextMatch != null)
content = SourceHtml.Substring(start, SourceHtml.IndexOf(NextMatch.Value) - start);
else
content = SourceHtml.Substring(start);

html.Append(content);
html.Append("<br>");
}
}
html.Append("</body></html>");

return html.ToString();
}
4、获取分页数据
public List<PageData> GetPages(string SourceHtml)
{
SourceHtml=SplitHtml(SourceHtml);

Regex r = new Regex(@"http:.+?((\[\d+\])|首页)");
MatchCollection ms = r.Matches(SourceHtml);

List<PageData> pages = new List<PageData>();

foreach (Match m in ms)
{
r = new Regex("><.+?>");
string s = r.Replace(m.Value, "");
s = s.Replace(">", "");

if (s.IndexOf("首页") >= 0)
pages.Add(new PageData(s.Substring(0, s.IndexOf("首页")),"首页"));
else
pages.Add(new PageData(s.Substring(0,s.IndexOf("[")),s.Substring(s.IndexOf("["))));
}

return pages;
}
完整源代码点击下载
1、从指定网址下载网页




















































































































完整源代码点击下载
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现