Archive

Posts Tagged ‘google parser in php’

Simple Google SERP Parser in PHP

February 10th, 2010 No comments

If you need an easy way to extract all links from Google SERP page, here is the script:

<?
set_time_limit(0);
// Here is our search term
$search=”Test search”;
// Let’s prepare it for Google
$slovo=urlencode(trim($search));
// Here comes the Google URL
$google=”http://www.google.com/search?q=”.$slovo.”&num=10″;
// Let’s place all the links into a single file.
$links=fopen(“$slovo.txt”,”a+”);
// Getting the page contents from Google
$content= @file_get_contents($google);
// Simple and dirtty regular expression, that does the job :) $matches is the result.
preg_match_all(‘/<h3><a href=”([^">]+)/’, $content, $matches);
// Here are our urls: let’s echo them and write to a file
foreach ($matches[1] as $url)
{
echo $url.”\r\n”;
fwrite($links, $url.”\r\n”);
}
fclose($links);

?>

It works at the moment; Google might change the SERP output format, then you will need to change the regexp. The rest is very simple, this script should work on any host, even without cURL support.