gpt4 book ai didi

httpclient - 无法使用 HttpClient 访问 google 趋势

转载 作者:行者123 更新时间:2023-12-02 08:17:59 30 4
gpt4 key购买 nike

我对此有点新手...基本上我需要运行一个脚本来从谷歌趋势下载.csv 文件。我按照这个reference写了下面的代码,代码如下:

     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、Auth的信息,但不知道如何使用这些信息。我想我应该在以下请求中添加这些 cookie,但不知 Prop 体如何添加。我编写了另一段代码来连接到特定 URL,但我不断收到此消息“您必须登录才能从 Google 趋势导出数据”。如果有帮助的话,代码在这里:

 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();

我四处搜寻,发现一些有用的信息,有人可以帮忙吗?

最佳答案

必须是 Java 语言吗?在Python中,它就像这样简单:

from pyGTrends import pyGTrends

connector = pyGTrends('google username','google password')
connector.download_report(('keyword1', 'keyword2'))
print connector.csv()

您需要 google trends api library .

如果必须是 Java,您可能需要查看 HttpClient examples来自 Apache 。 “基于表单的登录”和“客户端身份验证”可能都相关。

关于httpclient - 无法使用 HttpClient 访问 google 趋势,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112828/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com