2016-08-22 5 views
0

シンプルなRSSフィードのWebサイトを作成しようとしています。 RSSのほとんどの<img> xmlファイルのsrc値を取得するには?

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

タイトルとリンク作業のフィードが、画像や説明がない:
は、私はこれを行うことにより、RSSフィードの数を取得することができます。
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>.',... 

は、どのように私はそれから、画像のURLを取得することができますか?

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); 
     } 
    }) 

これは私のコードの一部であり、基本的に私は有線RSSから画像のURLを取得しようとしています。
私が 'image':item.image.urlを使用しても、動作しません。だから私はそれを何に変えるべきですか?

答えて

1

使用xml2js OPは、彼がPHPをしたかったと言った

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"]); 
}); 
+0

私はあなたの答えを試みたが、うまくいかなかった。私は自分のコードを編集して追加しました。 – Dan

+0

@ダンコードがあなたのために失敗したのは残念です....コードはどこで失敗しましたか?文字列から 'url'をフェッチするはずでした...また、 .... –

-1

DOMDocumentパーサを使用してイメージソースを取得できます。文字列の

$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" 
+0

をJSONにXMLを変換しますか? –

0

使用正規表現:アイデアは、正規表現を使用することです

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

Fiddle Demo

+0

私はあなたの答えを試みたが、うまくいかなかった。私は自分のコードを編集して追加しました。 – Dan

0

一つ。例の場合:

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