如何使用google api javascript获取多个网站供稿

时间:2014-02-03 07:17:48

标签: javascript rss

此功能没有采用这两个网站提要,只有第一个网址

function OnLoad() {
  // Create a feed instance that will grab Digg's feed.
  var feed = new google.feeds.Feed("http://www.tricks10.com/feed","http://liveurlifehere.com/blog/feed/");
  feed.setNumEntries(25);
  feed.includeHistoricalEntries();
  // Calling load sends the request off.  It requires a callback function.
  feed.load(feedLoaded);
}

2 个答案:

答案 0 :(得分:1)

答案很简单,你需要2个feed对象,因为Feed的构造函数需要一个简单的url。试试这个:

function loadFeed(url) {
  var feed = new google.feeds.Feed(url);
  feed.setNumEntries(25);
  feed.includeHistoricalEntries();
  feed.load(feedLoaded);
}

function OnLoad() {
  ["http://www.tricks10.com/feed", "http://liveurlifehere.com/blog/feed/"].map(loadFeed);
}

答案 1 :(得分:0)

它对我有用。我只需将google的代码与上面的答案合并。

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
            <script type="text/javascript">
                google.load("feeds", "1");
                function loadFeed(url) //for multiple urls
                   {
                    var feed = new google.feeds.Feed(url); //for multiple urls
                    feed.includeHistoricalEntries(); //include old stuff
                    feed.setNumEntries(15); //number of entries
                    feed.load(function (result) {
                        if (!result.error) 
                        {
                            result.feed.entries.forEach(function(entry)
                            {
                                var findImg = entry.content;
                                var img = $(findImg).find('img').eq(0).attr('src'); //i use jquery to find 1st img src
                                $('#feed').append('<div>your html here</div>');

                            });
                        }

                    });

                }
                google.setOnLoadCallback( function OnLoad() {
                ["http://firstURL.com/category/categoryname/feed/", "http://secondURL.com/category/categoryname/feed/, http://thirdURL.com/category/categoryname/feed/"].map(loadFeed);
                });

    </script>