【问题标题】:Perform google search programmatically using keywords使用关键字以编程方式执行 google 搜索
【发布时间】:2014-04-16 05:18:25
【问题描述】:

就像我们在 google 上通过关键字执行 google 搜索一样。

我们可以以编程方式进行这样的查询吗?喜欢

http://www.google.com/search?q=cupertino+american+food

执行查询后,我们应该获取每个链接的所有搜索结果详细信息以存储在数据库中。

就像一些网站提供 REST api 访问,这样用户就可以得到一堆结果他的查询。

无论是否使用谷歌,我都没有看到类似的事情。

【问题讨论】:

  • 您使用了google-search-api 标签,但您似乎没有看过API...

标签: php google-api google-search google-search-api


【解决方案1】:

无论您将使用何种技术,Google 都会阻止您的 IP 用于类似机器人的搜索查询。并且不要尝试使用 TOR 代理,因为他们的所有 IP 总是被禁止或挑战验证码。

您必须使用 Google API 才能符合 Google 的 T&C。结果也好很多

https://developers.google.com/custom-search/json-api/v1/overview

如果您拥有 CSE 并且每天限制为 100 个查询,则该 API 是免费的。如果您需要更多,每 1000 次查询需要支付 5 美元

【讨论】:

  • 谢谢,但为什么要投反对票?这个问题有什么问题吗?
【解决方案2】:

@mahtOrz:好的,这里有一些粗略的代码,可以将 Json 传递回控制台。请注意,api 基本搜索字符串不同于您所拥有的 www.google.com/search?q=cupertino+american+food。您需要使用下面的 Google API 基本 URL。你有你的 APIkey 和 CxKey 吗?如果没有,我也可以指导您完成这些步骤。

using System;
using System.Text;
using System.Net;
using System.IO;
using System.Web;

namespace GoogleSearchTest1
{
    class Program
    {
        //Google keys                                                   
        const string APIKey = "{your key here}";
        const string CSEKey = "{your key here}";
        //base url for the search query                                                 
        const string GoogleBaseURL = "https://www.googleapis.com/customsearch/v1?"; //per Google documentation

    public static void Main(string[] args)
    {
        string myQuery = "cupertino american food"; //put what you're searching for here
        string result = submitSearch(myQuery);
        Console.WriteLine(result);
        string dummy = Console.ReadLine();
    }
    public static string submitSearch(string myQuery)
    {
        try
        {    
            string final = string.Format(GoogleBaseURL+"key={0}&cx={1}&q={2}",
                HttpUtility.UrlEncode(APIKey),
                HttpUtility.UrlEncode(CSEKey),
                HttpUtility.UrlEncode(myQuery));
            final += "&alt=json";
            WebRequest myRequest = WebRequest.Create(final);
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
            Stream myStream = myResponse.GetResponseStream();
            StreamReader myReader = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);
            string result = myReader.ReadToEnd();
            myStream.Close();
            myReader.Close();
            myResponse.Close();
            return result;
        }
            catch (Exception e)
        {
            //debug statement       
        }           
        return null;
    }
}

}

【讨论】:

    【解决方案3】:

    使用cUrl请求,配合输出缓冲

    【讨论】:

    • 谢谢@maht0rz,有参考吗?
    【解决方案4】:

    @user123:如果你可以使用 C#,我可以提供一些建议吗? API 步骤非常广泛。让我知道!

    【讨论】:

    • 我在PHP工作,但如果逻辑相似,我可以理解它
    • 好的,这里有一些粗略的代码可以将 Json 传递回控制台。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2011-12-23
    • 2010-11-26
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多