首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用google api或任何其他技术解析RSS

如何使用google api或任何其他技术解析RSS
EN

Stack Overflow用户
提问于 2015-04-18 23:41:17
回答 1查看 140关注 0票数 0

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

代码语言:javascript
运行
复制
// 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');
});
EN

回答 1

Stack Overflow用户

发布于 2015-04-19 22:08:40

只需将此代码添加到您的脚本中

代码语言:javascript
运行
复制
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

代码示例:http://codepen.io/janih/pen/JdPMZX

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29719407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档