【问题标题】:How to parse the RSS using google api or any other techinques如何使用 google api 或任何其他技术解析 RSS
【发布时间】:2015-04-18 15:41:17
【问题描述】:

我想解析下面提到的 RSS 以获得标题、描述、图像和日期。目前我能够获得除图像之外的所有其他细节。我使用 google 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');
});

【问题讨论】:

  • 您的问题不清楚,您使用的是什么编程语言?你的代码呢?
  • 我正在使用 jquery。下面是我的代码。

标签: google-api rss


【解决方案1】:

只需将其添加到您的脚本中

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

【讨论】:

  • 太棒了,即使我看起来也一样。非常感谢 Janih :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多