2015-06-17 231 views
0

我正在使用以下代碼來顯示通過Javascript和Google Feed API將RSS源解析爲HTML。是否有可能從RSS提要中獲取圖像?它的標題,片段,日期和鏈接工作正常。我只需要圖片的網址。從RSS提取圖像Url FEED

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> 

回答

0

我能拉與內容(內設圖像標籤):VAR

content = entries[i].content; 

和分析,使用這種只獲取圖像:

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

      div.innerHTML = input; 

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