【问题标题】:download csv from google insight for search从谷歌洞察下载 csv 进行搜索
【发布时间】:2009-11-01 05:03:44
【问题描述】:

需要帮助编写脚本,使用 c# 从 google Insight 下载数据

这是下载地址,需要登录

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

如何输入我的用户名和密码?需要一些帮助,我是 C# 新手

【问题讨论】:

    标签: c# download


    【解决方案1】:

    要完成这项工作,您首先需要authenticate 以便为给定的可用于访问数据的谷歌网站获取有效的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);
            }
        }
    }
    

    【讨论】:

    • 完美!但是要获取字符串,请使用 Encoding.Unicode.GetString(csv);而不是 Encoding.UTF8.GetString(csv)
    • 嗨,从今天开始,它似乎不再工作了。 Clientlogin 方法未返回有关要创建的不同 cookie 的足够信息(与 ServiceLogin 相比),我们收到此消息“您已达到配额限制。请稍后再试。”
    • 可以肯定这不再起作用,因为 Google 已弃用它。
    【解决方案2】:

    好吧,这几天就变了。

    现在您必须通过身份验证而不是 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);
            }
        }
    }
    

    现在它又对我有用了。

    【讨论】:

    • 你知道如何从谷歌站长工具下载数据吗?喜欢每个网站的抓取问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-16
    • 2014-09-27
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多