Google search results with php

萝らか妹 提交于 2019-12-13 18:39:27

问题


I'm using the following php script to get search results from 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);

When this script runs in a cronjob (with 5 sec sleep) I receive a warning from Google and have to fill in a captcha (obvious). I always thought that using curl and a random user agent can avoid this. What is the correct solution?


回答1:


A better way to avoid captcha is to set a randomized sleep between 3-6 seconds per request.

Best solution is to use proxies.



来源:https://stackoverflow.com/questions/18682352/google-search-results-with-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!