Code: Number of Times a Term occurs in Google
October 28, 2007 | 5:49 pmJust some simple code to grab the number of times a term occurs on the web, according to Google. I found myself using this, and figured this’d be useful for others as well. And, I did this in C# rather than my usual darlin VB9 just for a change.
Regex r = new Regex(@”\<\/b\> of about \<b\>(?<count>.*)\<\/b\> for”); WebClient wc = new WebClient(); string url = “http://google.com/search?q=” + Uri.EscapeUriString(q); string page = wc.DownloadString(url); Match m = r.Match(page); if (m.Success) { Console.WriteLine(m.Groups[“count”].Value); } else { Console.WriteLine(0); } //No matches found,
Just put q into whatever term you want it to be, and you’re ready to go. Since the Google SOAP API was killed, scrapping is perhaps the fastest way to get data out of Google, and they have some of the most uglily structured code lying around. If I had first tried scrapping against Google instead of Wordpress when I started, I would’ve very certainly given up. It’s that bad.
And, btw, the last else is there to support searches for which there are no results. Most of the rest is self explanatory.
















