【问题标题】:Cannot access google trends using HttpClient无法使用 HttpClient 访问谷歌趋势
【发布时间】:2012-10-28 20:42:11
【问题描述】:

我是这方面的新手...基本上我需要运行一个脚本来从谷歌趋势下载 .csv 文件。我按照这个reference写了如下代码,代码是这样的:

     HttpClient client = new DefaultHttpClient();
     HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");

     try {

         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>;
         nameValuePairs.add(new BasicNameValuePair("Email", "myEmail"));
         nameValuePairs
                 .add(new BasicNameValuePair("Passwd", "myPasswd"));
         nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
         nameValuePairs.add(new BasicNameValuePair("source",
                 "Google-cURL-Example"));
         nameValuePairs.add(new BasicNameValuePair("service", "xapi"));

         post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
         HttpResponse response = client.execute(post);
         BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

         String line = "";
         while ((line = rd.readLine()) != null) {
             System.out.println(line);
             if (line.startsWith("SID=")) {
                 String key = line.substring(4);
                 // Do something with the key
         } catch (Exception e) {
                    }

我得到了有关 SID、LSID、Auth 的信息,但不知道如何使用这些信息。我想我应该在我的以下请求中添加这些 cookie,但不知道具体如何。我编写了另一段代码来连接到某个 URL,但我不断收到这条消息“您必须登录才能从 Google 趋势中导出数据”。如果有帮助,代码就在这里:

 URL url = new URL(myUrl);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();

        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setInstanceFollowRedirects(true);
        conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.addRequestProperty("Authorization", "SID"+key);
        conn.addRequestProperty("Email", "myEmail");
        conn.addRequestProperty("Passwd", "myPasswd");
        conn.setReadTimeout(5000);
        conn.connect();

我四处搜索,发现很少有用的信息,有人可以帮忙吗?

【问题讨论】:

    标签: httpclient apache-httpclient-4.x google-api-java-client


    【解决方案1】:

    它必须是Java吗?在python中,就这么简单:

    from pyGTrends import pyGTrends
    
    connector = pyGTrends('google username','google password')
    connector.download_report(('keyword1', 'keyword2'))
    print connector.csv()
    

    您需要google trends api library

    如果它必须是 Java,您可能需要查看来自 Apache 的 HttpClient examples。 “基于表单的登录”和“客户端身份验证”可能都相关。

    【讨论】:

      【解决方案2】:

      我刚刚编写了这个代码:

      https://github.com/elibus/j-google-trends-api

      它是 Google Trends API 的非官方 Java 实现。您可以使用它轻松访问 Google 趋势,或者您可能想查看代码以了解它是否有效。

      无论如何,身份验证流程如下(所有步骤都是必需的):

      1. 获取 https://accounts.google.com/ServiceLoginAuth 并解析 GALX id
      2. 发布用户名/密码 + GALX
      3. 获取http://www.google.com

      然后,您可以使用针对经过身份验证的用户的宽松 QoS 策略访问 Google Trend。

      【讨论】:

      猜你喜欢
      • 2021-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-08
      • 1970-01-01
      • 2013-10-04
      相关资源
      最近更新 更多