我编写了这段代码 - 一个Google搜索结果解析器 - 但它只获得了标题:
$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 />";
}
我如何获取网址和说明?
答案 0 :(得分:0)
您可以获取链接名称和网址,如此
示例:
<?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 />";
}