我是這種新手......基本上我需要運行腳本來從谷歌的趨勢下載.csv文件。我根據這個reference寫了下面的代碼,代碼如下:使用HttpClient無法訪問谷歌的趨勢
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://www.google.com/accounts/ClientLogin");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>;
nameValuePairs.add(new BasicNameValuePair("Email", "myEmail"));
nameValuePairs
.add(new BasicNameValuePair("Passwd", "myPasswd"));
nameValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
nameValuePairs.add(new BasicNameValuePair("source",
"Google-cURL-Example"));
nameValuePairs.add(new BasicNameValuePair("service", "xapi"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
if (line.startsWith("SID=")) {
String key = line.substring(4);
// Do something with the key
} catch (Exception e) {
}
我得到了SID,LSID,驗證信息,但不知道如何使用這些信息。我想我應該在我的以下請求中添加這些cookie,但不知道如何。我編寫了另一段代碼以連接到特定的URL,但我不斷收到此消息「您必須登錄才能從Google Trends中導出數據。」代碼是在這裏,如果它有幫助:
URL url = new URL(myUrl);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setInstanceFollowRedirects(true);
conn.addRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.addRequestProperty("Authorization", "SID"+key);
conn.addRequestProperty("Email", "myEmail");
conn.addRequestProperty("Passwd", "myPasswd");
conn.setReadTimeout(5000);
conn.connect();
我搜索周圍,發現幾個有用的信息,任何人都可以幫助?
我不想在瀏覽器上重定向登錄是否有任何解決方案? –
你是什麼意思?順便說一句,這裏有一個二進制客戶端:https://github.com/elibus/j-google-trends-client/tree/master/releases/org/freaknet/gtrends/client/j-google-trends-client/1.1 –