从rss命名空间元素中获取img,如<content:encoded>

时间:2015-12-08 10:49:47

标签: jquery rss

我正在尝试获取rss Feed的<content:encoded>元素中的第一个图片:

<item>
    <title>Time Travel Via Wormhole Breaks the Rules of Quantum Mechanics</title>
    <description>
        <![CDATA[<p>Science has done it again everybody! Brace yourselves for this groundbreaking news, freshly determined by physicists: Time travel, if it exists, may have some weird consequences. Gosh, who’d have thunk it? But no, seriously, a recent article suggests that a certain kind of theoretically possible time machine would wreak minor havoc with a firm principle [&#8230;]</p><p>The post <a href="http://blogs.discovermagazine.com/crux/2014/01/16/time-travel-via-wormhole-breaks-the-rules-of-quantum-mechanics/">Time Travel Via Wormhole Breaks the Rules of Quantum Mechanics</a> appeared first on <a href="http://blogs.discovermagazine.com/crux">The Crux</a>.</p>]]>
    </description>
    <content:encoded>
        <![CDATA[<p><img width="1170" height="669" src="http://www.futurity.org/wp/wp-content/uploads/2015/09/inuit_man2_1170.jpg" class="attachment-full wp-post-image" alt="Inuit man in Greenland" style="display:block;">
<p>Science has done it again everybody! Brace yourselves for this groundbreaking news, freshly determined by physicists: Time travel, if it exists, may have some weird consequences. Gosh, who’d have thunk it?</p>]]>
    </content:encoded>
</item>

然而,显然它并不那么简单 - 我尝试过这样的事情:

var entries = feed.entries;
var content = entries[i].content;
var img = $(content).find('content\\:encoded');

似乎返回了一些对象:[prevObject: o.fn.init[20], context: undefined, selector: "content\:encoded"]如何提取图像?

1 个答案:

答案 0 :(得分:0)

好的,我自己找到了这个解决方案src:

var content = entries[i].content;
var pattern = /<img[^>]+src="([^">]+)"/;
var img = pattern.exec(content);
console.log(img[1]);
相关问题