【问题标题】:Is there a more efficient way to get the number of search results from a google query?有没有更有效的方法来从谷歌查询中获取搜索结果的数量?
【发布时间】:2010-02-14 00:24:23
【问题描述】:

现在我正在使用这个代码:

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?

【问题讨论】:

    标签: c# json c#-4.0


    【解决方案1】:

    您已经参考了提到从 SOAP API 到 AJAX 的转换的帖子。

    RESTful interface 应该可以满足您的需求,因为它会限制返回的结果集,但会为您提供 estimatedResultCount 并且似乎不会引发任何法律问题(截至目前)。


    更新

    我从 Google 的 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");
    ...
    

    【讨论】:

    • 我无法弄清楚如何在我的应用程序中实际使用 ajax 查询。如果我使用:ajax.googleapis.com/ajax/services/search/… 我可以在页面源中看到estimatedResultCount,但是访问它的最佳方法是什么?
    • 如何从 JSON 中检索数据?
    猜你喜欢
    • 2020-05-24
    • 1970-01-01
    • 2012-10-04
    • 2019-04-10
    • 2016-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多