无法弄清楚如何使用PHP抓取谷歌的搜索结果

Ton*_*ite 0 php web-scraping

我正在尝试使用PHP来搜索Google搜索结果.

我尝试使用@file_get_contents(http://www.google.com/search?hl=en&q=test),但它不起作用.它仅适用于http://www.google.com.

我尝试使用curl代替.这是我的功能:

function my_fetch($url,$user_agent='Mozilla/4.0 (compatible; MSIE
5.01; Windows NT 5.0)')  { 
    $ch = curl_init(); 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent); 
    curl_setopt ($ch, CURLOPT_HEADER, 0); 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/'); 
    $result = curl_exec ($ch); 
    curl_close ($ch); 
    return $result;  }

$googleContent = my_fetch("http://www.google.com/search?hl=en&q=test");
echo $googleContent;
Run Code Online (Sandbox Code Playgroud)

结果是

302 Moved
The document has moved here.
Run Code Online (Sandbox Code Playgroud)

点击此处链接:http://www.google.com/sorry/?continue = http://www.google.com/search%3Fhl%3Den%26q%3Dtest

有没有办法使用PHP抓取搜索结果而无需学习API?

小智 6

您的问题是Google进行了重定向.你需要添加

CURLOPT_FOLLOWLOCATION => true
Run Code Online (Sandbox Code Playgroud)