使用PHP解析Google新闻RSS

sun*_*jie 3 php rss

我想用PHP解析Google News rss.我设法运行此代码:

<?
$news = simplexml_load_file('http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=n&output=rss');

foreach($news->channel->item as $item) {
    echo "<strong>" . $item->title . "</strong><br />";
    echo strip_tags($item->description) ."<br /><br />";
}
?>
Run Code Online (Sandbox Code Playgroud)

但是,我无法解决以下问题.例如:

  1. 我怎样才能获得新闻标题的超链接?
  2. 由于每个Google新闻在页脚中都有许多相关的新闻链接,(上面的代码也包括它们).如何从描述中删除它们?
  3. 我怎样才能得到每条新闻的图像?(Google显示每条新闻的缩略图)

谢谢.

spi*_*dEY 26

在那里,我们为您的特殊情况提供所需的信息:

<?php
$news = simplexml_load_file('http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&topic=n&output=rss');

$feeds = array();

$i = 0;

foreach ($news->channel->item as $item) 
{
    preg_match('@src="([^"]+)"@', $item->description, $match);
    $parts = explode('<font size="-1">', $item->description);

    $feeds[$i]['title'] = (string) $item->title;
    $feeds[$i]['link'] = (string) $item->link;
    $feeds[$i]['image'] = $match[1];
    $feeds[$i]['site_title'] = strip_tags($parts[1]);
    $feeds[$i]['story'] = strip_tags($parts[2]);

    $i++;
}

echo '<pre>';
print_r($feeds);
echo '</pre>';
?>
Run Code Online (Sandbox Code Playgroud)

输出应该如下所示:

[2] => Array
        (
            [title] => Los Alamos Nuclear Lab Under Siege From Wildfire - ABC News
            [link] => http://news.google.com/news/url?sa=t&fd=R&usg=AFQjCNGxBe4YsZArH0kSwEjq_zDm_h-N4A&url=http://abcnews.go.com/Technology/wireStory?id%3D13951623
            [image] => http://nt2.ggpht.com/news/tbn/OhH43xORRwiW1M/6.jpg
            [site_title] => ABC News
            [story] => A wildfire burning near the desert birthplace of the atomic bomb advanced on the Los Alamos laboratory and thousands of outdoor drums of plutonium-contaminated waste Tuesday as authorities stepped up ...
        )
Run Code Online (Sandbox Code Playgroud)