2009-11-01 24 views

Trả lời

7

Để thực hiện công việc này, trước tiên bạn cần authenticate để nhận SID hợp lệ cho trang web đã cho có thể được sử dụng để truy cập dữ liệu. Dưới đây là cách bạn có thể đạt được điều này:

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 protected]&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); 
     } 
    } 
} 
+0

Hoàn hảo! Nhưng để có được chuỗi sử dụng Encoding.Unicode.GetString (csv); thay vì Encoding.UTF8.GetString (csv) – Dragouf

+0

Xin chào, vì hôm nay có vẻ như nó không hoạt động nữa. Phương thức Clientlogin không trả lại đủ thông tin về các cookie khác nhau để tạo (so với ServiceLogin) và chúng tôi nhận được thông báo này "Bạn đã đạt đến giới hạn hạn ngạch của mình. Vui lòng thử lại sau". – Dragouf

+0

Khá chắc chắn điều này không hoạt động nữa vì Google đã không dùng nữa. –

1

Ok nó đã thay đổi kể từ vài ngày.

Bây giờ bạn phải vượt qua auth chứ không phải SID.

Vì vậy, mã bây giờ là:

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 protected]&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); 
     } 
    } 
} 

Và bây giờ nó hoạt động trở lại đối với tôi.

+0

Bạn có biết cách tải xuống dữ liệu từ công cụ quản trị trang web của google không? Giống như vấn đề thu thập dữ liệu cho mỗi trang web? – Mithil

+0

Điều này không hoạt động nữa. –