Justin
Justin

Reputation: 2479

Is there a more efficient way to get the number of search results from a google query?

Right now I am using this code:

string url = "http://www.google.com/search?sourceid=chrome&ie=UTF-8&q=hey&esrch=FT1";
string source = getPageSource(url);
string[] stringSeparators = new string[] {
 "<b>",
 "</b>"
};
string[] b = source.Split(stringSeparators, StringSplitOptions.None);
bool isResultNum = false;
foreach(string s in b) {
 if (isResultNum) {
  MessageBox.Show(s.Replace(",", ""));
  return;
 }
 if (s.Contains(" of about ")) {
  isResultNum = true;
 }
}

Unfortunately it is very slow, is there a better way to do it? Also is it legal to query google like this? From the answer in this question it didn't sound like it was How to download Google search results?

Upvotes: 0

Views: 1710

Answers (1)

Filburt
Filburt

Reputation: 18061

You already referenced the post mentioning the transition from SOAP API to AJAX.

The RESTful interface should give you what you need since it limits the returned results sets but gives you estimatedResultCount and doesn't seem to raise any legal issues (as of now).


Update

I followed the link from Googles API page to www.json.org and found a link to this library on sourceforge. I did not try it out myself yet but i think it will be helpful for you.

Update 2

It looks like Json.Net offers better support than csjson.

Json.NET sample

...
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(googleUri);
request.Referer = "http://www.your-referer.com";
HttpWebResponse response = (HttpWebResponse)req.GetResponse();
Stream responsestream = response.GetResponseStream();
StreamReader responsereader = new StreamReader(responsestream);
JObject jo = JObject.Parse(responsereader.ReadToEnd());
int resultcount = (int)jo.SelectToken("responseData.cursor.estimatedResultCount");
...

Upvotes: 3

Related Questions