如何利用PHP从各大搜索引擎获取最新热点新闻?


随着互联网的发展,人们获取信息的途径也变得越来越多样化,当我们想要了解当下热门的新闻、话题时,搜索引擎是最受欢迎的一种途径之一。各大搜索引擎都会根据用户的搜索关键词推荐一些热门资讯。本文将介绍如何使用 PHP 程序从各大搜索引擎获取热门资讯,并合并展示四大搜索引擎热门新闻标题和URL的列表结果。

在本示例中,我们将从四个搜索引擎获取热门资讯:GoogleBing Yahoo 这些搜索引擎都提供了 RSS 或 Atom 格式的订阅服务,我们可以使用 PHP 程序获取这些订阅服务中的热门资讯。而 Baidu 我们可以使用正则的方式获取,以下是这些搜索引擎的订阅服务地址:

Google: https://trends.google.com/trends/hottrends/atom/feed
Bing: https://www.bing.com/news/search?q=*&format=RSS
Yahoo: https://www.yahoo.com/news/rss/mostviewed
Baidu: https://top.baidu.com/board?tab=realtime

在本示例中,我们使用了一个关联数组来存储这些搜索引擎的名称和对应的订阅服务地址:

$search_engines = array(
    'Google' => 'https://trends.google.com/trends/hottrends/atom/feed',
    'Bing' => 'https://www.bing.com/news/search?q=*&format=RSS',
    'Yahoo' => 'https://www.yahoo.com/news/rss/mostviewed',
    'Baidu' => 'https://top.baidu.com/board?tab=realtime',
);

使用 cURL 获取 HTML 内容

使用 PHP 中的 cURL 扩展可以方便地获取远程 URL 的内容。在本示例中,我们使用 cURL 获取了 Google、Bing 和 Yahoo 的订阅服务中的 HTML 内容:

// 使用cURL获取HTML内容
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$html = curl_exec($ch);
curl_close($ch);

使用正则表达式解析 Baidu 的 HTML 内容

与其他搜索引擎不同,百度的热门资讯不是通过 RSS 或 Atom 格式的订阅服务提供的,而是通过一个类似 JSON 的格式返回的 HTML 页面提供的。因此,我们需要使用正则表达式来解析这个 HTML 页面,获取其中的热门资讯信息。以下是解析百度 HTML 页面的示例代码:

if ($engine == 'Baidu') {
    preg_match_all('#query":"(.*?)","rawUrl":"(.*?)",#', $html, $matches);
    foreach ($matches[1] as $key => $title) {
        $link = $matches[2][$key];
        echo "<a href='{$link}'>{$title}</a><br />";
    }
}

对于 Google、Bing 和Yahoo 等搜索引擎,我们可以使用 PHP 中的 SimpleXML 扩展来解析其 RSS 或 Atom 格式的订阅服务。SimpleXML 扩展提供了一种方便的方式,可以将 XML 文档转换为对象或数组,从而便于获取其中的元素。以下是使用 SimpleXML 解析 Google、Bing 和 Yahoo 的订阅服务的示例代码:

  $xml = simplexml_load_string($html);
        foreach ($xml->channel->item as $item) {
        echo "<a href='{$item->link}'>{$item->title}</a><br />";
        }

最后,我们将所有搜索引擎的热门资讯输出到页面中,展示给用户。以下是将所有搜索引擎的热门资讯输出到页面的。

完整示例代码:

$search_engines = array(
    'Google' => 'https://trends.google.com/trends/hottrends/atom/feed',
    'Bing' => 'https://www.bing.com/news/search?q=*&format=RSS',
    'Yahoo' => 'https://www.yahoo.com/news/rss/mostviewed',
    'Baidu' => 'https://top.baidu.com/board?tab=realtime',
);

foreach ($search_engines as $engine => $url) {
 echo "<h2>{$engine}</h2>";

    // Use cURL to fetch the HTML content
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $html = curl_exec($ch);
    curl_close($ch);

    // Parse the HTML using regular expressions
    if ($engine == 'Baidu') {
        preg_match_all('#query":"(.*?)","rawUrl":"(.*?)",#', $html, $matches);
        foreach ($matches[1] as $key => $title) {
            $link = $matches[2][$key];
            echo "<a href='{$link}'>{$title}</a><br />";
        }
    } else {
        // Use SimpleXML to parse other search engine's RSS feed
        $xml = simplexml_load_string($html);
        foreach ($xml->channel->item as $item) {
            echo "<a href='{$item->link}'>{$item->title}</a><br />";
        }
    }
}


示例效果截图:


在这篇文章中,我们将介绍如何使用PHP从各大搜索引擎获取最新热点新闻。我们将使用一个包含四个主流搜索引擎URL的数组,然后使用PHP的cURL库获取各自的HTML内容,并使用正则表达式或SimpleXML解析内容,最终展示新闻标题和链接。

演示地址:http://img.dujup.com/hot-words.php