使用PHP解析Google新闻RSS [英] Parsing Google News RSS with PHP

查看:52
本文介绍了使用PHP解析Google新闻RSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用PHP解析Google新闻rss.我设法运行了这段代码:

I want to parse Google News rss with PHP. I managed to run this code:

<?
$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 />";
}
?>

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

However, I'm unable to solve following problems. For example:

  1. 如何获取新闻标题的超链接?
  2. 由于每个Google新闻的页脚中都有许多相关的新闻链接,(并且我上面的代码也包含了这些新闻链接).如何从说明中删除这些内容?
  3. 我还如何获得每个新闻的图像? (Google显示每个新闻的缩略图)

谢谢.

推荐答案

我们来了,正是您针对特定情况所需要的:

There we go, just what you need for your particular situation:

<?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>';
?>

输出应如下所示:

[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 ...
        )

这篇关于使用PHP解析Google新闻RSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆