首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> .NET 網頁程式設計 >> ASP.NET 基礎 >> 關於ASP.NET

自建博客園評論 RSS 訂閱服務:HTML 解析生成 RSS 2.0

     編輯:關於ASP.NET

博客園缺評論 RSS,利用 .NET 3.5 SyndicationFeed + WebClient + 正則解析 HTML,快速構建個人評論訂閱工具。

架構設計

  1. 前端頁面:輸入文章 URL,JS 跳轉至 CommentRss.ashx?url=...
  2. HTTP Handler:
    • WebClient.DownloadString 獲取 HTML
    • 正則提取評論:標題、鏈接、時間、作者、內容
    • 構建 SyndicationFeed 對象
    • Rss20FeedFormatter 輸出 RSS 2.0 XML

核心代碼

public class CommentRss : IHttpHandler {
    public void ProcessRequest(HttpContext context) {
        string url = context.Request.QueryString["url"];
        string html = new WebClient { Encoding = Encoding.UTF8 }.DownloadString(url);
        var feed = ParseHtmlToFeed(url, html); // 正則解析
        context.Response.ContentType = "text/xml";
        new Rss20FeedFormatter(feed).WriteTo(XmlWriter.Create(context.Response.Output));
    }
    // ParseHtmlToFeed 使用 Regex 匹配評論列表結構
}

關鍵點

  • .NET 3.5 內置 System.ServiceModel.Syndication 命名空間原生支持 RSS/Atom
  • 博客園模板變更會導致正則失效,可擴展參數指定解析策略
  • 純個人工具,不考慮性能/擴展/容錯,極簡實用
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved