title . ""; -6ren">
gpt4 book ai didi

php - 用 PHP 解析谷歌新闻 RSS

转载 作者:可可西里 更新时间:2023-10-31 23:51:39 33 4
gpt4 key购买 nike

我想用 PHP 解析 Google 新闻 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 />";
}
?>

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

  1. 如何获取新闻标题的超链接?
  2. 因为每个 Google 新闻在页脚中都有许多相关的新闻链接(我上面的代码也包括它们)。如何从描述中删除这些内容?
  3. 我怎样才能得到每条新闻的图像? (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 ...
)

关于php - 用 PHP 解析谷歌新闻 RSS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6512318/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com