RSS:概念與實作
09 Mar 2014什麼是 RSS?為什麼要用 RSS?
RSS(Really Simple Syndication,簡易資訊聚合) 一種消息來源格式規範,用以蒐集經常更新內容的網站的最新資訊,例如部落格、新聞、多媒體等。其目的是把內容「送」到使用者的面前。RSS 可藉由各種閱讀器來閱讀。
RSS vs Sitemap
就定義上來說
- Sitemap:網站地圖,目的是完整呈現網站的所有內容
- RSS:列出新增或更新項目
當 GoogleBot 來檢索網站時,你最希望它看到?不會是整個網站全部的內容吧,應該是整個網站最重要或最近更新的項目,藉此將更新的內容提交給搜尋引擎。因此實作 RSS 只提交更新的頁面是比較有用的,做全站的 Sitemap 不見得有太大的意義(而且目前的網站份量都是很大的)。
RSS 實作範例 - 使用 ASP.NET MVC (C#)
以下的步驟大致上就是如何產生 item(想要分享的文章)、設定 channel 名稱、連結、logo、語系、更新時間等重要資訊,然後再寫入 xml 檔中。
public class Feed
{
public static void UpdateFeed(HttpRequest req)
{
string baseUrl = req.Url.Scheme + "://" + req.Url.Authority;
#region load records
List<Article> articles = new List<Article>();
#region fake data
Article ar1 = new Article();
ar1.id = 1;
ar1.title = "波蘭的饕餮、飢餓與美食復興";
ar1.link = "http://cn.tmagazine.com/food-wine/20140308/tc08recipe/zh-hant";
ar1.description = "波蘭飲食文化經歷過講求豐盛的「饕餮」時期,味寡色黯的社會主義「糊口飯」年代,直至如今的美食振興運動。美食家陳楠回顧了波蘭飲食文化,並分享兩道地道波蘭美食食譜。";
ar1.pubDate = DateTime.Now;
ar1.imgSource = "http://g1.nytimg.com/images/2014/03/07/world/tc08recipe/tc08recipe-articleLarge.png";
ar1.imgSize = 1;
articles.Add(ar1);
Article ar2 = new Article();
ar2.id = 2;
ar2.title = "金融危機五年後,冰島依然後怕";
ar2.link = "http://cn.nytimes.com/business/20140308/c08icebanks/zh-hant";
ar2.description = "冰島的經歷是一場活生生的實驗,它驗證了在全球金融危機之下,一個國家迫使自己的金融企業破產而不救援可能會發生什麼情況。";
ar2.pubDate = DateTime.Now;
ar2.imgSource = "http://graphics8.nytimes.com/images/2014/01/16/business/16icebanks-ss-slide-O7ZN/16icebanks-ss-slide-O7ZN-articleLarge.jpg";
ar2.imgSize = 1;
articles.Add(ar2);
Article ar3 = new Article();
ar3.id = 3;
ar3.title = "《冰雪奇緣》助迪士尼重奪動畫之王寶座";
ar3.link = "http://cn.tmagazine.com/culture/20140305/t05disney/zh-hant";
ar3.description = "迪士尼堪稱主流動畫的誕生地,但在電腦動畫方面遭遇瓶頸,讓後來居上的皮克斯多年佔據奧斯卡獎。從收購皮克斯、公司重組到推出《冰雪奇緣》,迪士尼一步步奪回霸主地位。";
ar3.pubDate = DateTime.Now;
ar3.imgSource = "http://g1.nytimg.com/images/2014/02/06/business/05disney/06disney-articleLarge.jpg";
ar3.imgSize = 1;
articles.Add(ar3);
#endregion fake data
#endregion
#region create feed
SyndicationFeed feed = new SyndicationFeed();
//set properties on the feed
feed = new SyndicationFeed("紐約時報中文網", "紐約時報中文網是紐約時報公司旗下的首個中文媒介産品,紐約時報中文網旨在向中國讀者提供有關全球時事、商業及文化的高水準報導。", new Uri("http://cn.nytimes.com/zh-hans"), baseUrl, DateTime.Now);
feed.Id = baseUrl;
feed.Copyright = new TextSyndicationContent("紐約時報中文網");
feed.LastUpdatedTime = new DateTimeOffset(DateTime.Now);
feed.Language = "zh-tw";
//add feed logo url
string imageUrl = "http://cn.nytimes.com/img/nameplate.gif";
feed.ImageUrl = new Uri(imageUrl);
//add the URL that will link to the published feed when it's done
SyndicationLink link = new SyndicationLink(new Uri(baseUrl + "/feed.xml"));
link.RelationshipType = "self";
link.MediaType = "text/html";
link.Title = "紐約時報中文網";
feed.Links.Add(link);
#endregion
#region loop over the entries to add feed items
List<SyndicationItem> items = new List<SyndicationItem>();
int maxItems = 1000;
for (int i = 0; i < articles.Count; i++)
{
Article a = articles[i];
TextSyndicationContent content = new TextSyndicationContent(a.description);
Uri uri = new Uri(a.link);
SyndicationItem item = new SyndicationItem(a.title, content, uri, a.id.ToString(), DateTime.Now);
item.PublishDate = a.pubDate;
item.ElementExtensions.Add(
new XElement("enclosure",
new XAttribute("type", "image/jpeg"),
new XAttribute("url", a.imgSource),
new XAttribute("length", a.imgSize)
).CreateReader()
);
items.Add(item);
//stop after adding the max desired number of items
if (items.Count >= maxItems)
{
break;
}
}
feed.Items = items;
#endregion
#region output feed to a file
var rssFormatter = new Rss20FeedFormatter(feed, false);
var output = new StringBuilder();
using (var writer = XmlWriter.Create(@"C:\feed.xml", new XmlWriterSettings { Indent = true }))
{
rssFormatter.WriteTo(writer);
writer.Flush();
}
#endregion
}//end UpdateFeed
public class Article
{
public int id { get; set; }
public string title { get; set; }
public string link { get; set; }
public string description { get; set; }
public DateTime pubDate { get; set; }
public string imgSource { get; set; }
public long imgSize { get; set; }
}
}
結果:輸出一個 feed.xml 檔案。
備註
- 參考自 Easily Build An Atom or RSS Feed With C# and the Syndication Namespace。這篇文章對於重點程式碼也有解說,有興趣的可以看一下。
- 如果對欄位定義或格式有興趣,可以參考 RSS Advisory Board,因為不同性質的網站對於欄位的選擇會有些許差異。
驗證
產生的 RSS 檔案記得要做驗證。
驗證工具
如何讓 Bot 看見 RSS?
如果希望搜尋引擎檢索,或使用者能利用 Feedly 等閱讀器訂閱該網站的 RSS,可以放在這些地方:
- 串接 Google Webmaster API 定期提交
- Search Console「Sitemap」提交
- 在 robots.txt 設定
Sitemap:http://www.example.com/feed.xml
- 放在
<head>
中:<link rel="alternate" type="application/rss+xml" href="feed.xml" title="RSS Feed">
- footer 的任一連結:
<a type="application/rss+xml" href="feed.xml">RSS Feed</a>
後記
(2017/03/28 更新)
這篇文章主要目的是介紹 RSS 的功用和實作方法,也就是當時為了粉多任務實作 RSS 的筆記。
關於 SEO 操作相關可參考這篇文章-RSS 與 SEO。
推薦閱讀
這篇文章的原始位置在這裡-RSS:概念與實作
由於部落格搬遷至此,因此在這裡放了一份,以便閱讀;部份文章片段也做了些許修改,以期提供更好的內容。