tags:

views:

84

answers:

1

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 http://stackoverflow.com/questions/903747/how-to-download-google-search-results

+3  A: 

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");
...
Filburt
Justin
How can I retrieve the data from the JSON?
Justin
Thank yo so much!
Justin