用php搜索谷歌搜索结果

时间:2013-09-08 09:45:40

标签: php curl simple-html-dom

我使用以下php脚本从Google获取搜索结果。

include("simple_html_dom.php");
include("random-user-agent.php");


$query = 'facebook';

$curl = curl_init();  
curl_setopt($curl, CURLOPT_URL, 'http://www.google.com/search?q='.$query.'');   
#curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_USERAGENT,random_user_agent());
$str = curl_exec($curl);   
curl_close($curl);   

$html= str_get_html($str);  

$i = 0;
foreach($html->find('li[class=g]') as $element) {
    foreach($element->find('h3') as $item) 
    {
        $title[$i] = ''.$item->plaintext.'' ;
    }
       $i++;
}
print_r($title);

当这个脚本在cronjob(睡眠时间为5秒)中运行时,我会收到Google发出的警告,并且必须填写验证码(显而易见)。我一直认为使用curl和随机用户代理可以避免这种情况。什么是正确的解决方案?

1 个答案:

答案 0 :(得分:1)

避免验证码的更好方法是在每个请求的3-6秒之间设置随机睡眠。

最佳解决方案是使用代理。