1

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

4

1 回答 1

1

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

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

于 2014-03-03T16:13:46.463 回答