如何使用谷歌API或任何其他技术解析RSS

时间:2015-04-18 15:41:17

标签: google-api rss

我想解析下面提到的RSS,以获取标题,描述,图像和日期。目前我能够获得除图像之外的所有其他细节。我使用谷歌api feed来解析rss。在这种情况下,请任何人都可以。 RSS:https://news.google.com/news/feeds?cf=all&ned=in&hl=en&q=cricket&output=rss

// Google Feed API: https://developers.google.com/feed/
// Inspiration: http://designshack.net/articles/javascript/build-an-automated-rss-feed-list-with-jquery/

function parseFeed(url, container) {
    $.ajax({
        url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=5&callback=?&q=' + encodeURIComponent(url),
        dataType: 'json',
        success: function (data) {
            // log object data in console
            console.log(data.responseData.feed);
            // append feed link and title in container
            $(container).append('<a href="' + url + '"><span class="iconicstroke-rss-alt"></span></a>');
            $(container).append('<h1 class="feed">' + data.responseData.feed.title + '</h1>');
            // for each entry... *
            $.each(data.responseData.feed.entries, function (key, value) {
                // * create new date object and pass in entry date
                var date = new Date(value.publishedDate);
               // var thumbnail = entry.mediaGroups[0].contents[0].url;
                // * create months array
                var months = new Array(12);
                months[0] = 'January';
                months[1] = 'February';
                months[2] = 'March';
                months[3] = 'April';
                months[4] = 'May';
                months[5] = 'June';
                months[6] = 'July';
                months[7] = 'August';
                months[8] = 'September';
                months[9] = 'October';
                months[10] = 'November';
                months[11] = 'December';
                // * parse month, day and year
                var month = date.getMonth();
                var day = date.getDate();
                var year = date.getFullYear();

                // * assign entry variables
                var title = '<h3 class="title"><a href="' + value.link + '" target="_blank">' + value.title + '</a></h3>';
                var time = '<p class="time">' + months[month] + ' ' + day + ', ' + year + '</p>';
                var snippet = '<p class="snippet">' + value.contentSnippet + '</p>';
                var img = '<p class="snippet">' + value.thumbnail + '</p>';
                var entry = '<div class="entry">' + title + time + snippet + '</div>';
                // * append entire entry in container
                $(container).append(entry);
            });
        },
        // if there's an error... *
        error: function (errorThrown) {
            // * log error message in console
            console.log(errorThrown);
            // * show error message
            alert('Houston, we have a problem.');
        }
    });
}

$(document).ready(function () {
    parseFeed('https://news.google.com/news/feeds?pz=1&cf=all&ned=en&hl=in&q=aishwarya%20rai&output=rss', '#csstricks');
});

1 个答案:

答案 0 :(得分:0)

只需将其添加到您的脚本

即可
var content = document.createElement("content");
content.innerHTML = value.content; 
var images = "";
$(content).find('img').each(function() {  
    images += this.outerHTML;
});
var img = '<p class="snippet">' + images + '</p>';

答案与此处相同:https://stackoverflow.com/a/26369373/989257

Codepen示例:http://codepen.io/janih/pen/JdPMZX