Scraping Google Results

Scraping the results of a Google search into a comma-delimited file.

Because you can use the Google API to get results and put them in any format you like, why in the world would you bother to do a manual search result on Google, save the results, and then scrape them with a Perl program? You might not want, or indeed be able, to do anything as fancy as the Google API allows; you might just want to grab some results, drop them into a spreadsheet, and go.

Just like we did in Peeling Phone Numbers [Hack #49], you can save Google web search results to a file, and then process them into a comma-delimited text file with a short Perl script.


Be sure to set your preferences [Hack #1] to 100 results per page to get the most out of this hack.

The Code

# # Google Web Search Results exported to CSV suitable
# for import into Excel 
# Usage: perl < results.html > results.csv

print qq{"title","url","size","domain suffix"\n};

my($results) = (join '', <>) =~

while ( $results =~ 
m!<p><a href="?(.+?)"?>(.+?)</a>.+?\s+-\s+(\d+k)?!mgis
) { 
  my($url,$title, $size) = ($1||'',$2||'',$3||''); 
  my($suffix) = $url =~ m!\.(\w+)/!; 
  $title =~ s!"!""!g;         # double escape " marks 
  $title =~ s!<.+?>!!g; # drop all HTML tags
  print qq{"$title","$url","$size","$suffix"\n};

Running the Script

Run the script from the command line, specifying the result’s HTML filename and name of the CSV file you wish to create or to which you wish to append additional results. For example, using results.html as input and results.csv as output:

$ perl < results.html > results.csv

Leaving off the > and CSV filename sends the results to the screen for your perusal:

$ perl < results.html

The Results

Here’s a sample run on the results of a search for Mac OS X:

$ perl < results.html
"title","url","size","domain suffix"
"Apple - Mac OS X","","","com"
"Apple - Software - Mac OS X Server","",
"Mac OS X Development","","28k","com"
"Mac OS X Hints - Get the most from X!","",
"Mac OS X Apps - The Source For Mac OS X Software",
" - free Macintosh software downloads for Mac 
OS ... ","","101k","com"
"O'Reilly Mac OS X Conference",
"MacNN | OS X","","94k","com"
"???? - Mac OS X","","43k","jp"
"Apple - Support - Mac OS X",

You’ll see that the program records four attributes to the CSV file: title, URL, size (when available), and top-level domain. The “snippet” of web page usually included with a Google result was omitted, because it’s difficult to read in a spreadsheet format.

So why include the page size and domain? Research. If you’re generating a set of results to be referred to later, it’s handy to be able to sort them by suffix. “edu” results tend to be different from “org” results, which tend to be different from “com” results, and so on. Not to mention differing result sets by country, .uk versus .jp, for instance. And if you’re generating links to contact later (to ask for a reciprocal link, for example), it’s handy to be able to set apart the less-commercial suffixes such as .edu and .org.

Get Google Hacks now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.