从RSS FEED拉出图像URL

时间:2015-06-17 19:56:33

标签: javascript rss google-feed-api

我使用以下代码显示,以通过Javascript和Google Feed API将RSS Feed解析为HTML。是否可以从RSS源中获取图像?标题,片段,日期和链接正常工作。我只需要图像的URL。

    function myGetElementsByClassName(selector) {
    if ( document.getElementsByClassName ) {
        return document.getElementsByClassName(selector);
    }

    var returnList = new Array();
    var nodes = document.getElementsByTagName('div');
    var max = nodes.length;
    for ( var i = 0; i < max; i++ ) {
        if ( nodes[i].className == selector ) {
            returnList[returnList.length] = nodes[i];
        }
    }
    return returnList;
}

var rssReader = {
    containers : null,

    // initialization function
    init : function(selector) {
        containers = myGetElementsByClassName(selector);
        for(i=0;i<containers.length;i++){
            // getting necessary variables
            var rssUrl = containers[i].getAttribute('rss_url');
            var num = containers[i].getAttribute('rss_num');
            var id = containers[i].getAttribute('id');
            // creating temp scripts which will help us to transform XML (RSS) to JSON
            var url = encodeURIComponent(rssUrl);
            var googUrl = 'https://ajax.googleapis.com/ajax/services/feed/load?v=1.0&num='+num+'&q='+url+'&callback=rssReader.parse&context='+id;

            var script = document.createElement('script');
            script.setAttribute('type','text/javascript');
            script.setAttribute('charset','utf-8');
            script.setAttribute('src',googUrl);
            containers[i].appendChild(script);
        }
    },

    // parsing of results by google
    parse : function(context, data) {
        var container = document.getElementById(context);
        container.innerHTML = '';

        // creating list of elements
        var mainList = document.createElement('ul');

        // also creating its childs (subitems)
        var entries = data.feed.entries;
        for (var i=0; i<entries.length; i++) {
            var listItem = document.createElement('li');
            var title = entries[i].title;
            var publishedDate  = entries[i]. publishedDate;
            var contentSnippet = entries[i].contentSnippet;
            var contentSnippetText = document.createTextNode(contentSnippet);
            var mediaGroup = entries[i].mediaGroup;
            var link = document.createElement('a');
            link.setAttribute('href', entries[i].link);
            link.setAttribute('target','_blank');
            var text = document.createTextNode(title);
            link.appendChild(text);

            var text = document.createTextNode(publishedDate);
            link.appendChild(text);


                var text = document.createTextNode(mediaGroup);
            link.appendChild(text);

            // add link to list item
            listItem.appendChild(link);

            var desc = document.createElement('p');
            desc.appendChild(contentSnippetText);

            // add description to list item
            listItem.appendChild(desc);

            // adding list item to main list
            mainList.appendChild(listItem);
        }
        container.appendChild(mainList);
    }
};

window.onload = function() {
    rssReader.init('post_results');
}



<div class="post_results" id="post_results2" rss_num="2" rss_url="http://www.feed.com/feed">
        <div class="loading_rss">
            <img alt="Loading..." src="images/loading.gif" />
        </div>
    </div>

1 个答案:

答案 0 :(得分:0)

我能够使用:var

来提取内容(包含图像标记)
content = entries[i].content;

并使用以下方法解析图像以获取图像:

   var input = content;
            var div = document.createElement('div');

            div.innerHTML = input;

            var img = div.getElementsByTagName('img')[0];