views:

348

answers:

3

I need to supply a keyword like "blue metal kettle" (with/without quotes) and get only the number of results found for this search. If I search without quotes right now, I get:

 Results 1 - 10 of about 1,040,000 for blue metal kettle. (0.19 seconds)

Here '1,040,000' is the number I want. Is there any API function to do this, or I must extract this number through the HTML? What's the best way to do this?

A: 

Google closed their API for public use quite some time ago. If you want to use a supported API, check out the bing api which provides this as part of its results. OTherwise, you're down to scraping the HTML. Be aware that you've shown formatting for us/english numbers. THe formatting will vary on other google sites and/or your profile settings and/or your browser settings.

No Refunds No Returns
http://www.bing.com/developers/
No Refunds No Returns
this answer is inaccurate since google does support a new api called ajax search api now
Click Upvote
the ajax API only supports search results, not the detailed information requested on total count.
No Refunds No Returns
+1  A: 

You can screen scrape it. Something like:

$keywords = "blue metal kettle";
$html = file_get_contents("http://www.google.com/?q=" . rawurlencode($keywords));
preg_match('/Results 1 - \d+ of about ([0-9,]+) for/', $html, $reg);
var_dump($reg[1]);

If you use this in an application, you would probably be violating Google's terms of use.

troelskn
+1  A: 

From the Google Ajax API, there's a estimatedResultsCount property in JSON, but you can read about unresolved complaints filed on the issue tracker:

Result count varies
http://code.google.com/p/google-ajax-apis/issues/detail?id=32

(I see the Question is tagged PHP, but client-side javascript in conjunction may be of interest.)

micahwittman
thanks, the ajax api can actually also be called thru php
Click Upvote
Good point. Glad to help.
micahwittman