加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用PHP解析Google新闻RSS

发布时间:2020-12-13 22:22:44 所属栏目:PHP教程 来源:网络整理
导读:我想用 PHP解析Google News rss.我设法运行此代码: ?$news = simplexml_load_file('http://news.google.com/news?pz=1cf=allned=ushl=entopic=noutput=rss');foreach($news-channel-item as $item) { echo "strong" . $item-title . "/strongbr /"; echo st
我想用 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 />";
}
?>

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

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

谢谢.

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读