【发布时间】: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# 从 google Insight 下载数据
这是下载地址,需要登录
http://www.google.com/insights/search/overviewReport?q=test&cmpt=q&content=1&export=2
如何输入我的用户名和密码?需要一些帮助,我是 C# 新手
【问题讨论】:
要完成这项工作,您首先需要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);
}
}
}
【讨论】:
好吧,这几天就变了。
现在您必须通过身份验证而不是 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);
}
}
}
现在它又对我有用了。
【讨论】: