Pull RSS Feeds From Facebook Page

南笙酒味 提交于 2019-12-20 14:20:28

问题


I need help to pull RSS feeds from a facebook page I'm using the following code but it keeps giving me an error :

string url = 
    "https://www.facebook.com/feeds/page.php?id=40796308305&format=rss20";

XmlReaderSettings settings = 
    new XmlReaderSettings
                    {
                         XmlResolver = null,
                         DtdProcessing=DtdProcessing.Parse,

                     }; 
XmlReader reader = XmlReader.Create(url,settings);

SyndicationFeed feed = SyndicationFeed.Load(reader);

foreach (var item in feed.Items)
{
    Console.WriteLine(item.Id);
    Console.WriteLine(item.Title.Text);
    Console.WriteLine(item.Summary.Text);

}

if (reader != null) reader.Close();

This code works perfectly with any blog or page rss but with Facebook rss it give an exception with the following message

The element with name 'html' and namespace 'http://www.w3.org/1999/xhtml' is not an allowed feed format.

Thanks


回答1:


Facebook will return HTML in this instance because it doesn't like the User Agent supplied by XmlReader. Since you can't customize it, you will need a different solution to grab the feed. This should solve your problem:

var req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.UserAgent = "Fiddler";

var rep = req.GetResponse();
var reader = XmlReader.Create(rep.GetResponseStream());

SyndicationFeed feed = SyndicationFeed.Load(reader);

This is strictly a behavior of Facebook, but the proposed change should work equally well for other sites that are okay with your current implementation.




回答2:


It works when using Gregorys code above if you change the feed format to atom10 instead of rss20. Change the url:

string url = 
"https://www.facebook.com/feeds/page.php?id=40796308305&format=atom10";



回答3:


In my case also Facebook feed was difficult to consume and then I try with feedburner to burn the feed for my facebook page. Feedburner generated the feed for me in Atom1.0 format. And then I successfully :) consumed this with system.syndication class my code was:

string  Main()
   {
       var url = "http://feeds.feedburner.com/Per.........all";


       Atom10FeedFormatter formatter = new Atom10FeedFormatter();
       using (XmlReader reader = XmlReader.Create(url))
       {
           formatter.ReadFrom(reader);
       }
       var s = "";
       foreach (SyndicationItem item in formatter.Feed.Items)
       {
           s+=String.Format("[{0}][{1}] {2}", item.PublishDate, item.Title.Text, ((TextSyndicationContent)item.Content).Text);
       }

       return s;
   }


来源:https://stackoverflow.com/questions/6294948/pull-rss-feeds-from-facebook-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!