drghhp8706
2014-01-26 07:43
浏览 89
已采纳

使用来自rss Feed的php抓取图像

PHP

$rss = new DOMDocument();
$rss->load('http://www.autoexpress.co.uk/feeds/all');
$feed = array();
foreach ($rss->getElementsByTagName('item') as $node) {
  $item = array (
    'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
    'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
    'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
    'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
    'image' => $node->getElementsByTagName('whattagdoiaddhere')->item(0)->nodeValue,
  );
  array_push($feed, $item);
}
$limit = 3;
for($x=0;$x<$limit;$x++) {
  $title = str_replace(' & ', ' &amp; ', $feed[$x]['title']);
  $link = $feed[$x]['link'];
  $description = $feed[$x]['desc'];
  $date = date('l F d, Y', strtotime($feed[$x]['date']));
  echo '<div class="news-row-index">';
  echo '<div class="img"><a href="'.$link.'" target="_blank" title="'.$title.'"><img src="'.$image.'" height="79" width="89"></a></div>';
  echo '<div class="details-index"><p><h5><a href="'.$link.'" target="_blank" title="'.$title.'">'.$title.'</a></h5><br />';
  echo '<small><em>Posted on '.$date.'</em></small></p>';
  echo '<p>'.$description.'</p></div>';
  echo '</div>';
}
  1. I am trying to get the images from this rss but don't know exactly how to get the image and add it into the <div class="img".
  2. Also i have a problem because the image is being received but it's located in the $description variable.
  3. Tried searching and found php rss feed show images but when i add the code from that answer I get nothing in the rss module. Please help me I really appreciate it.

图片转代码服务由CSDN问答提供 功能建议

PHP

  $ rss = new DOMDocument(); \  n $ rss-&gt; load('http://www.autoexpress.co.uk/feeds/all'); 
 $ feed = array(); 
foreach($ rss-&gt; getElementsByTagName('item')  as $ node){
 $ item = array(
'title'=&gt; $ node-&gt; getElementsByTagName('title') - &gt; item(0) - &gt; nodeValue,
'desc'=&gt  ; $ node-&gt; getElementsByTagName('description') - &gt; item(0) - &gt; nodeValue,
'link'=&gt; $ node-&gt; getElementsByTagName('link') - &gt; item(0)  - &gt; nodeValue,
'date'=&gt; $ node-&gt; getElementsByTagName('pubDate') - &gt; item(0) - &gt; nodeValue,
'image'=&gt; $ node-&gt; getElementsByTagName  ('whattagdoiaddhere') - &gt; item(0) - &gt; nodeValue,
); 
 array_push($ feed,$ item); 
} 
 $ limit = 3; 
for($ x = 0;  $ x&lt; $ limit; $ x ++){
 $ title = str_replace('&amp;','&amp; amp;',$ feed [$ x] ['title']); 
 $ link = $ feed [  $ x] ['link']; 
 $ description = $ feed [$ x] ['desc']; 
 $ date = date('l F d,Y',strtotime($ feed [$ x] [  'date'])); 
 echo'&lt; div class  =“news-row-index”&gt;'; 
 echo'&lt; div class =“img”&gt;&lt; a href =“'。$ link。'”target =“_ blank”title =“'。$ 标题。'“&gt;&lt; img src =”'。$ image。'“height =”79“width =”89“&gt;&lt; / a&gt;&lt; / div&gt;'; 
 echo'&lt; div  class =“details-index”&gt;&lt; p&gt;&lt; h5&gt;&lt; a href =“'。$ link。'”target =“_ blank”title =“'。$ title。'”&gt;'。$ 标题。'&lt; / a&gt;&lt; / h5&gt;&lt; br /&gt;'; 
 echo'&lt; small&gt;&lt; em&gt;张贴于'。$ date。'&lt; / em&gt;&lt; / small&gt  ;&lt; / p&gt;'; 
 echo'&lt; p&gt;'。$ description。'&lt; / p&gt;&lt; / div&gt;'; 
 echo'&lt; / div&gt;'; 
} 
    
 
 
  1. 我正在尝试从此RSS 但不知道如何获取图像并将其添加到&lt; div class =“img”
  2. 我也遇到问题,因为正在接收图片,但它位于 $ description 变量中。
  3. 尝试搜索并找到 php rss feed show images 但是当我从该答案中添加代码时,我在rss模块中什么都没有。 请帮助我,我真的很感激。
  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douchen7555 2014-01-26 08:11
    已采纳

    You can make use of DomDocument loadHTML to parse the description field and grab the image tags.

    foreach ($rss->getElementsByTagName('item') as $node) {
      $htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
      $html = new DOMDocument();        
      $html->loadHTML($htmlStr);
       //get the first image tag from the description HTML
       $img = $html->getElementsByTagName('img')->item(0)->getAttribute('src');
       $item = array (
        'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
        'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
        'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
        'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
        'image' => $img,
      );
      array_push($feed, $item);
    }
      print_r($feed);
    

    Also to check if the DOMNodeList is empty, try

    foreach ($rss->getElementsByTagName('item') as $node) {
          $htmlStr = $node->getElementsByTagName('description')->item(0)->nodeValue;
          $html = new DOMDocument();        
          $html->loadHTML($htmlStr);
           //get the first image tag from the description HTML
            $imgTag = $html->getElementsByTagName('img');
            $img = ($imgTag->length==0)?'noimg.png':$imgTag->item(0)->getAttribute('src');
           $item = array (
            'title' => $node->getElementsByTagName('title')->item(0)->nodeValue,
            'desc' => $node->getElementsByTagName('description')->item(0)->nodeValue,
            'link' => $node->getElementsByTagName('link')->item(0)->nodeValue,
            'date' => $node->getElementsByTagName('pubDate')->item(0)->nodeValue,
            'image' => $img,
          );
          array_push($feed, $item);
        }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题