Javascript 从RSS/Atom提要中提取图像

Javascript 从RSS/Atom提要中提取图像,javascript,jquery,xml,rss,rss-reader,Javascript,Jquery,Xml,Rss,Rss Reader,我想知道如何从RSS和Atom提要中提取图像,以便在容器中显示提要的相对标题、描述和链接时,可以将它们用作缩略图。到目前为止,我的代码(如下所示)只从某些提要类型获取图像,我想知道如何才能获取脚本遇到的每个图像 if (feed_image_type == "description") { item_img = $($(this).find('description').text()).find("img").attr("src"); } else if (feed_image_type

我想知道如何从RSS和Atom提要中提取图像,以便在容器中显示提要的相对标题、描述和链接时,可以将它们用作缩略图。到目前为止,我的代码(如下所示)只从某些提要类型获取图像,我想知道如何才能获取脚本遇到的每个图像

if (feed_image_type == "description") {
    item_img = $($(this).find('description').text()).find("img").attr("src");
} else if (feed_image_type == "encoded") {
    item_img = $($(this).find('encoded').text()).find("img").attr("src");
} else if (feed_image_type == "thumbnail") {
    item_img = $(this).find('thumbnail').attr('url');
} else {
    item_img = $(this).find('enclosure').attr('url');
}
例如,我不知道如何从下面的代码rss提要片段中获取图像链接:

<description>
  <![CDATA[
   <img src="https://i.kinja-img.com/gawker-media/image/upload/s--E93LuLOd--/c_fit,fl_progressive,q_80,w_636/hd6cujrvf1d72sbxsbnr.jpg" /><p>With a surprise showing of skill and, at one point, a miracle, the bottom-ranked team in the European <em>League </em>Championship Series will not end the summer winless.<br></p><p><a href="http://compete.kotaku.com/european-league-team-finally-wins-its-first-series-of-t-1797363638">Read more...</a></p>
  ]]>
</description>

欧洲联赛冠军系列赛中排名垫底的球队,凭借令人惊讶的技术表现和奇迹,不会以这个夏天的不败告终。

]]>
使用以下来源:

通过将
数据类型设置为
“XML”
,将内容正确地设置为XML是必不可少的

此代码是独立的,适用于:

var xmlString = '<Customer><![CDATA[ <img src="y1" /> ]]></Customer>';
var xmlObj = $.parseXML(xmlString);
var cdataText = xmlObj.firstChild.firstChild.textContent;
var jqueryObj = $(cdataText);
var imgUrl = jqueryObj.find('img').attr('src');
console.log(imgUrl);
这应该很接近:

if (feed_image_type == "description") {
    var cdataText = $(this).firstChild.firstChild.textContent;
    var jqueryObj = $(cdataText);
    item_img = jqueryObj.find('img').attr('src');
}
你也可以试试这个

let str = `<description>
  <![CDATA[
   <img src="https://i.kinja-img.com/gawker-media/image/upload/s--E93LuLOd--/c_fit,fl_progressive,q_80,w_636/hd6cujrvf1d72sbxsbnr.jpg" /><p>With a surprise showing of skill and, at one point, a miracle, the bottom-ranked team in the European <em>League </em>Championship Series will not end the summer winless.<br></p><p><a href="http://compete.kotaku.com/european-league-team-finally-wins-its-first-series-of-t-1797363638">Read more...</a></p>
  ]]>
</description>`;

//We need to strip CDATA in our case. Otherwise the parser will not parse the contents inside it.
str = str.replace("<![CDATA[", "").replace("]]>", "")
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(str,"text/xml");
let images = [...xmlDoc.querySelectorAll('img')].map(image=>image.getAttribute('src'))
let str=`
欧洲联赛冠军系列赛中排名垫底的球队,凭借令人惊讶的技术表现和奇迹,不会以这个夏天的不败告终。

]]> `; //在我们的案例中,我们需要剥离CDATA。否则,解析器将不会解析其中的内容。 str=str.replace(“,”) 让parser=newdomparser(); 让xmlDoc=parser.parseFromString(str,“text/xml”); 让images=[…xmlDoc.querySelectorAll('img')].map(image=>image.getAttribute('src'))
您是否尝试过
$(this).find('img').attr('src')
let str = `<description>
  <![CDATA[
   <img src="https://i.kinja-img.com/gawker-media/image/upload/s--E93LuLOd--/c_fit,fl_progressive,q_80,w_636/hd6cujrvf1d72sbxsbnr.jpg" /><p>With a surprise showing of skill and, at one point, a miracle, the bottom-ranked team in the European <em>League </em>Championship Series will not end the summer winless.<br></p><p><a href="http://compete.kotaku.com/european-league-team-finally-wins-its-first-series-of-t-1797363638">Read more...</a></p>
  ]]>
</description>`;

//We need to strip CDATA in our case. Otherwise the parser will not parse the contents inside it.
str = str.replace("<![CDATA[", "").replace("]]>", "")
let parser = new DOMParser();
let xmlDoc = parser.parseFromString(str,"text/xml");
let images = [...xmlDoc.querySelectorAll('img')].map(image=>image.getAttribute('src'))