是否有更有效的方法从谷歌查询中获取搜索结果的数量?

时间:2010-02-14 00:24:23

标签: c# json c#-4.0

现在我正在使用此代码:

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;
                }
            }

不幸的是它很慢,有没有更好的方法呢?像这样查询谷歌也是合法的吗?从这个问题的答案来看,它听起来并不像是How to download Google search results?

1 个答案:

答案 0 :(得分:3)

您已经引用了提及从SOAP API到AJAX的过渡的帖子。

RESTful interface应该会为您提供所需内容,因为它限制了返回的结果集,但却为您提供了 estimatedResultCount ,并且似乎没有引起任何法律问题(截至目前)。< / p>


<强>更新

我按照Googles API页面到www.json.org的链接找到了link to this library on sourceforge。我自己没有尝试过,但我认为这对你有帮助。

更新2

看起来Json.Net比csjson提供更好的支持。

Json.NET示例

...
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");
...