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.
  • 写回答

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);
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?