从谷歌洞察下载csv进行搜索

时间:2009-11-01 05:03:44

标签: c# download

需要帮助编写脚本使用c#

从google insight下载数据

这是下载网址,需要登录

http://www.google.com/insights/search/overviewReport?q=test&cmpt=q&content=1&export=2

如何输入用户名和密码?需要一些新的帮助c#

2 个答案:

答案 0 :(得分:7)

要完成这项工作,您需要先authenticate才能获得可用于访问数据的特定Google网站的有效SID。以下是您如何实现这一目标:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            // TODO: put your real email and password in the request string
            var response = client.DownloadString("https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email=youraccount@gmail.com&Passwd=secret&service=trendspro&source=test-test-v1");
            // The SID is the first line in the response
            var sid = response.Split('\n')[0];
            client.Headers.Add("Cookie", sid);
            byte[] csv = client.DownloadData("http://www.google.com/insights/search/overviewReport?q=test&cmpt=q&content=1&export=2");

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

答案 1 :(得分:1)

好吧,几天后就改变了。

现在你必须传递auth而不是SID。

所以代码现在是:

class Program
{
    static void Main(string[] args)
    {
        using (var client = new WebClient())
        {
            // TODO: put your real email and password in the request string
            var response = client.DownloadString("https://www.google.com/accounts/ClientLogin?accountType=GOOGLE&Email=youraccount@gmail.com&Passwd=secret&service=trendspro&source=test-test-v1");
            // The Auth line
            var auth = response.Split('\n')[2];
            client.Headers.Add("Authorization", "GoogleLogin " + auth);
            byte[] csv = client.DownloadData("http://www.google.com/insights/search/overviewReport?q=test&cmpt=q&content=1&export=2");

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

现在它再次为我工作。