使用 AngularJS 从<description> RSS 提要中提取图像

Extracting image from <description> RSS feed with AngularJS

本文关键字:图像 RSS 提取 description AngularJS 使用      更新时间:2023-09-26

我正在尝试学习AngularJS,同时构建一个简单的RSS提要。我设法发出了一个JSON请求并检索了我解析的所有数据,标题,链接,描述和所有RSS。我像这样提取图像:

angular.forEach(feed.entries, function(value){
        value.myImage = $(value.content).find('img').eq(0).attr('src');
        console.log('image' + value.myImage);
}

但是,有些提要具有不同的结构,而是将图像放置在<content> </content>像这样,图像放置在<description> </description>中,如下所示,我正在尝试以这种方式获得它:

angular.forEach(feed.entries, function(value){
    value.myImage = $(value.description).find('img').eq(0).attr('src');
    console.log('no image' + " " + value.myImage);
});

但是我得到的只是未定义的,所以我无法显示此提要中的图像。我尝试了不同的方法,但我真的很迷茫,我不确定我在这里做错了什么。

我已经在谷歌上搜索并尝试了几个星期。任何帮助都非常感谢!

谢谢:)


这是我得到的 JSON 对象。

Object {feedUrl: "https://www.behance.net/rss", title: "Behance Featured Projects", link: "http://www.behance.net", author: "", description: "The latest projects featured on the Behance"…}
$$hashKey: "027"
author: ""
description: "The latest projects featured on the Behance"
entries: Array[10]
0: Object
$$hashKey: "029"
author: ""
categories: Array[0]
content: "<img src="https://mir-s3-cdn-cf.behance.net/projects/404/2e322a33145177.Y3JvcCw4NzYsNjg0LDAsNzc.jpg" style="float:left;margin-right:15px"><br> Various Illustrations by Patryk Hardziej / 2015"
contentSnippet: " Various Illustrations by Patryk Hardziej / 2015"
link: "https://www.behance.net/gallery/33145177/Various-Illustrations-2015"
publishedDate: "Thu, 28 Jan 2016 08:00:02 -0800"
sImage: undefined
title: "Various Illustrations 2015"

好的,我想我发现它:)

从查询中获取的 HTML 如下所示

<img src="..."> Some text <br/> Some text ...

find 方法仅适用于查找子节点。在这种情况下,img 不是 value.content 的子项,因此它不返回任何内容。

你可以做

angular.forEach(feed.entries, function(value){
  // force creation of a root node -> find method should works for all cases
  var content = '<div>'+value.content+'</div>';
  value.myImage = $(content).find('img').eq(0).attr('src');
  console.log('image' + value.myImage);
}