2

我最近两天搜索答案,但似乎找不到(确定的)答案。我需要从 Google 趋势中获取 CSV 数据,为此我需要登录到我的 Google 帐户。到目前为止,我尝试使用以下代码(最初由Oauth 谷歌趋势下载 CSV 文件中的 @user1883833 发布:

static void Main(string[] args)
{
    using (var client = new WebClient())
    {
        var terms = new List<string>() {"debt", "profit", "euro", "dollar", "financial", "economy", "federal reserve", "earnings", "fed", "consumer spending" , "employment", "unemployment", "jobs" };
        var username = "your username";
        var password = "password";

        var response = client.DownloadString(string.Format("https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email={0}&Passwd={1}&service=trendspro&source=test-test-v1", username, password));

        // The SID is the first line in the response
        // The Auth line
        var auth = response.Split('\n')[2];
        client.Headers.Add("Authorization", "GoogleLogin " + auth);

        int i = 1;
        while (terms.Count > 0)
        {
            // google limits 5 sets of terms per request
            var arr = terms.Take(5).ToArray();
            terms = terms.Skip(5).ToList();

            var joined = string.Join("%2C%20", arr);
            byte[] csv = client.DownloadData(string.Format("http://www.google.com/trends/trendsReport?hl=en-US&q={0}&cmpt=q&content=1&export=1", joined));

            // TODO: do something with the downloaded csv file:
            Console.WriteLine(Encoding.UTF8.GetString(csv));
            File.WriteAllBytes(string.Format("report{0}.csv", i), csv);
            i++;
        }

    }
}

虽然上述工作正常 - 它只允许在“您已达到配额限制。请稍后再试”之前进行少量下载。消息,然后 Google 似乎会阻止 IP 很长一段时间(24 小时或更长时间 - 但只能通过代码,您仍然可以在同一登录时从网站下载数据,此限制可以通过代理绕过 - 但同样,只有很少的查询可以在达到限制之前处理)。

据我所见 - 以上登录,不会生成谷歌在谷歌趋势中使用的 cookie。我知道我可以使用 DotNetOpenAuth 登录,但问题是:我可以只使用代码登录,而不重定向到谷歌登录页面吗?我的意思是:

   string username="username";
   string password="password";
   DotNetOpenAuth.GoogleLogin(username,password);

可能吗?如果没有 - 是否有任何其他登录方式可以允许正常的配额限制?

提前致谢。

此致。

4

0 回答 0