Php 除了标题之外,还可以获取谷歌搜索结果的URL和描述

Php 除了标题之外,还可以获取谷歌搜索结果的URL和描述,php,libcurl,Php,Libcurl,我写了这段代码——谷歌搜索结果解析器——但它只得到标题: $url = "http://www.google.com/search?client=opera&q=example&sourceid=opera&ie=UTF-8&oe=UTF-8"; $ch = curl_init(); $timeout = 5; curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER,

我写了这段代码——谷歌搜索结果解析器——但它只得到标题:

$url = "http://www.google.com/search?client=opera&q=example&sourceid=opera&ie=UTF-8&oe=UTF-8";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);
curl_close($ch);

$dom = new DOMDocument();
@$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('h3') as $link) {
    echo $link->nodeValue;
    echo "<br />";
}

如何获取url和描述?

您可以像这样获取链接名称和url-s

例如:

<?php
$url = "http://www.google.com/search?client=opera&q=example&sourceid=opera&ie=UTF-8&oe=UTF-8";
$ch = curl_init();
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT, $timeout);
$html = curl_exec($ch);


curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($html);

foreach ($dom->getElementsByTagName('h3') as $link) {
    echo $link->nodeValue."\n";
    echo str_replace('/url?q=', '',$link->firstChild->getAttribute('href'))."\n";

    echo "<br />";
}

你做了些什么来尝试获取URL/描述?这段代码:foreach$dom->getElementsByTagName'a'as$link{echo$link->attributes->getNamedItemhref->value;}但是我得到了很多链接谷歌不喜欢被刮,这是针对他们的TOS的,他们会主动阻止你。经过多次尝试,您将看到验证码或IP封锁,我知道。我不要求这个。你能告诉我如何使用上述方法获得描述吗?