简体   繁体   English

如何获得 <img> xml文件中的src值?

[英]How to get <img> src value from xml file?

I am trying to create a simple rss feed website. 我正在尝试创建一个简单的rss feed网站。
I can get a few of rss feeds by just doing this: 通过执行以下操作,我可以获得一些rss feed:

let article = {
              'title': item.title,
              'image': item.image.url,
              'link': item.link,
              'description': item.description,
           }

Title and link work for most of rss feeds, but image and description do not. 标题和链接适用于大多数rss提要,但图像和描述则不行。
Since a lot of rss fees has image as html inside of description like this: 由于很多rss费用的描述内都有html格式的图像,如下所示:

{ title: 'The Rio Olympics Are Where TV Finally Sees the Future',
description: '<div class="rss_thumbnail"><img src="http://www.wired.com/wp-content/uploads/2016/08/GettyImages-587338962-660x435.jpg" alt="The Rio Olympics Are Where TV Finally Sees the Future" /></div>Time was, watching the Olympics just meant turning on your TV. That\'s changed—and there\'s no going back. The post <a href="http://www.wired.com/2016/08/rio-olympics-tv-finally-sees-future/">The Rio Olympics Are Where TV Finally Sees the Future</a> appeared first on <a href="http://www.wired.com">WIRED</a>.',...

How can I get image's url from it? 如何从中获取图片的网址?

EDIT: 编辑:

http.get("http://www.wired.com/feed/"...

  .on('readable', function()  {
        let stream = this;
        let item;
        while( item = stream.read()){
           let article = {
              'title': item.title,
              'image': item.image.url,
              'link': item.link,
              'description': item.description,
           }
           news.push(article);
        }
  })  

this is some of my codes, and basically I am trying to get image url from Wired rss. 这是我的一些代码,基本上我正在尝试从Wired rss获取图像url。
If I user 'image': item.image.url, it does not work. 如果我使用'image':item.image.url,则无法使用。 So what should I change it to? 那么我应该将其更改为什么?

use xml2js for converting xml to json 使用xml2js将xml转换为json

var parseString = require('xml2js').parseString;

var xml = '<img title=\'A San Bernardino County Fire Department firefighter watches a helitanker make a water drop on a wildfire, seen from Cajon Boulevard in Devore, Calif., Thursday, Aug. 18, 2016. (David Pardo/The Daily Press via AP)\' height=\'259\' alt=\'APTOPIX California Wildfires\' width=\'460\' src=\'http://i.cbc.ca/1.3730399.1471835992!/cpImage/httpImage/image.jpg_gen/derivatives/16x9_460/aptopix-california-wildfires.jpg\' />';

parseString(xml, function (err, result) {
    console.log(JSON.stringify(result, null, 4));
    console.log(result["img"]["$"]["src"]);
});

Use regex of string: 使用字符串的正则表达式:

var res = description.match(/src=.*\.(jpg|jpeg|png|gif)/gi);

Fiddle Demo 小提琴演示

One idea would be to use regular expressions. 一种想法是使用正则表达式。 For ex: 例如:

var re = /(src=)(\\'htt.*\\')/g
var img_string = "your image tag string"
var match = re.exec(img_string)
var result = match[1]

You can use DOMDocument parser to get Image source. 您可以使用DOMDocument解析器获取Image源。

$html = "<img title=\'A San Bernardino County Fire Department firefighter watches a helitanker make a water drop on a wildfire, seen from Cajon Boulevard in Devore, Calif., Thursday, Aug. 18, 2016. (David Pardo/The Daily Press via AP)\' height=\'259\' alt=\'APTOPIX California Wildfires\' width=\'460\' src=\'http://i.cbc.ca/1.3730399.1471835992!/cpImage/httpImage/image.jpg_gen/derivatives/16x9_460/aptopix-california-wildfires.jpg\' />";

$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);
$src = $xpath->evaluate("string(//img/@src)"); # "/images/image.jpg"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM