谷歌搜索结果与php


Google search results with php

我正在使用以下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 和随机用户代理可以避免这种情况。正确的解决方案是什么?

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

最好的解决方案是使用代理。