Skip to Content
Google Hacks
book

Google Hacks

by Tara Calishain, Rael Dornfest
February 2003
Intermediate to advanced
352 pages
9h
English
O'Reilly Media, Inc.
Content preview from Google Hacks

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.

Tip

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

The Code

#!/usr/bin/perl
# google2csv.pl # Google Web Search Results exported to CSV suitable
# for import into Excel 
# Usage: perl google2csv.pl < results.html > results.csv

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

my($results) = (join '', <>) =~
  m!<div>(.*?)</div>!mis;

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Google Hacks, 2nd Edition

Google Hacks, 2nd Edition

Tara Calishain, Rael Dornfest
Google Hacks, 3rd Edition

Google Hacks, 3rd Edition

Rael Dornfest, Paul Bausch, Tara Calishain
Open source at AWS

Open source at AWS

Adrian Cockcroft
Algorithms of the Intelligent Web

Algorithms of the Intelligent Web

Dmitry Babenko, Haralambos Marmanis

Publisher Resources

ISBN: 0596004478