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.







The Google SOAP API is still available. The guy who
Prolific Programmer | October 29, 2007 | 2:29 amThe Google SOAP API is still available. The guy who wrote it left Google and therefore it won’t be maintained anymore.
@Profilic Programmer: Yep, it's still available. But, no new API
yuvipanda | October 29, 2007 | 2:31 am@Profilic Programmer: Yep, it’s still available. But, no new API keys will be issued, which basically means it’s dead…
Now, I’d love to see the Perl one-liner that does this
^ or the javascript ones 8 ) especially with the
Bhasker V K | October 29, 2007 | 10:07 am^ or the javascript ones 8 ) especially with the versatility that you get with firebug’s console.
Keep Clicking,
Bhasker V Kode
The Javascript API isn't an API: It's just a library.
yuvipanda | October 29, 2007 | 12:30 pmThe Javascript API isn’t an API: It’s just a library. I can’t use it from VB9 or C#, for example. For all intents and purposes, there’s no Google Search API right now folks.
@YuviPanda any API is no more than a library.
Prolific Programmer | October 29, 2007 | 12:37 pm@YuviPanda
any API is no more than a library.
nah, with an API (a real, Web API), my chosen
yuvipanda | October 29, 2007 | 12:42 pmnah, with an API (a real, Web API), my chosen language doesn’t matter at all. I could just as well use LOLCODE for it. But with a Library, you’re tied to the language which the library supports. Different, no?
You need protocol support in LOLCODE for whatever web API
Prolific Programmer | October 29, 2007 | 3:03 pmYou need protocol support in LOLCODE for whatever web API uses, i.e. HTTP requests, which are far easier to do in some languages than others. See my blog for some ruby code to manipulate twitter into doing what I want, for example of a language that makes web manipulation easy.
To a certain extent, you’re spot on that you are tied to the langauge which the library supports. However, every language I know of has a C API, at some level. Therefore one can design wrappers around a given C library in python, perl, ruby, Java, etc.
Granted, I don’t know every language in existence and I don’t know .Net at all
Yes, but have you looked at the Google Search Javascript
yuvipanda | October 29, 2007 | 3:10 pmYes, but have you looked at the Google Search Javascript “API”? It’s a mass of obfuscated javascript, more like a traditional obfuscated compiled dll. You have pretty much zero chance of using it from anything else, unless you do something like embed IE (or Gecko) into your code (via C) and access it via that. This involves multiple levels of cross-language calls, which are as easy as biting off pizza with your mouth closed.
Bottom line: The Google Search “API” is tied to just Javascript, and so is just a library, not an API. Agreed?
Hey Yuvi, I guess you are aware of the Live Search
Ahmad | October 30, 2007 | 5:19 pmHey Yuvi,
I guess you are aware of the Live Search 2.0 Wave and yea they do provide a sexy Search API. Stay away from Evil
http://msdn2.microsoft.com/en-us/library/bb266180.aspx
Wow, thanks for pointin THAT out Ahmed :) Sounds cool,
yuvipanda | October 30, 2007 | 5:22 pmWow, thanks for pointin THAT out Ahmed
Sounds cool, will check that out 