【问题标题】:What has to be done to have a Java program search Google?必须做什么才能让 Java 程序搜索 Google?
【发布时间】:2013-02-20 06:49:22
【问题描述】:

这是一个想法:

  1. 程序(Java 语言)将接受输入
  2. 程序接受输入并在网上某处使用它来获取结果
  3. 它把上网的结果显示在程序界面上

这有点像您可以通过 Google 桌面应用程序而不是浏览器进行搜索?

我只需要在这方面朝着正确的方向大步前进。 (也许我应该寻找某种方法)我对 Java API 不是很熟悉。

【问题讨论】:

  • 您能否更清楚地了解您要与之通信的 API 是什么?这个问题没有一般的答案。
  • 我想我需要一些动作监听器以及输入并定义一些动作,这将使程序接受输入并使用它从网站检索数据。
  • 来自哪个网站?请具体说明。
  • 我在看usingenglish.com/members/text-analysis上的文本分析工具
  • AFAIU Google 删除了对其搜索 API 的访问权限,并且不鼓励抓取 Google 页面以获取点击量。

标签: java connection google-search


【解决方案1】:

您可以使用 Java 的标准 HttpURLConnection 来搜索内容。然后解析响应只需要Apache tika,它用于从 HTML 页面中提取文本。

这是一个使用 Url Connection 的简单示例:

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder;

public class SimpleHTTPRequest {

  /**
   * @param args
   */
  public static void main(String[] args) {
      HttpURLConnection connection = null;
      DataOutputStream wr = null;
      BufferedReader rd  = null;
      StringBuilder sb = null;
      String line = null;

      URL serverAddress = null;

      try {
          serverAddress = new URL("http://www.google.com/search?q=test");
          //set up out communications stuff
          connection = null;

          //Set up the initial connection
          connection = (HttpURLConnection)serverAddress.openConnection();
          connection.setRequestMethod("GET");
          connection.setDoOutput(true);
          connection.setDoInput(true);
          connection.setUseCaches(false);
          connection.setRequestProperty ( "Content-type","text/xml" ); 
          connection.setAllowUserInteraction(false);
          String strData = URLEncoder.encode("test","UTF-8");
          connection.setRequestProperty ( "Content-length", "" + strData.length ());  
          connection.setReadTimeout(10000);
          connection.connect();

          //get the output stream writer and write the output to the server
          //not needed in this example
          wr = new DataOutputStream(connection.getOutputStream());
          wr.writeBytes("q="+strData);
          wr.flush();

          //read the result from the server
          rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
          sb = new StringBuilder();

          while ((line = rd.readLine()) != null)
          {
              sb.append(line + '\n');
          }

          System.out.println(sb.toString());

      } catch (MalformedURLException e) {
          e.printStackTrace();
      } catch (ProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      finally
      {
          //close the connection, set all objects to null
          connection.disconnect();
          rd = null;
          sb = null;
          wr = null;
          connection = null;
      }
  }
}

here 你找到了一个使用 apache tika 提取文本的示例

【讨论】:

    【解决方案2】:

    您必须了解 Java 套接字编程以及 Web 服务器的工作原理。除此之外,您还可以使用HttpURLConnection 类建立与 Web 服务器的连接,然后您就可以下载内容了。

    http://docs.oracle.com/javase/1.4.2/docs/api/java/net/HttpURLConnection.html

    【讨论】:

      【解决方案3】:

      您可以使用开源库Apache Http components。这使工作变得轻松。

      【讨论】:

        【解决方案4】:

        你必须使用 URL 类来连接网络。

        例如

                url1 = new URL(url);
                InputStream input=url1.openStream();
                BufferedInputStream bis=new BufferedInputStream(input);
                dis=new DataInputStream(bis);
               // byte[] buffer=new byte[1000];
                String data="";
                while(dis.available()!=0)
                {
                    data+=dis.readLine();
                }
        
                jobj=new JSONObject(data);
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-06-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-11-15
          • 1970-01-01
          • 2010-09-22
          • 1970-01-01
          相关资源
          最近更新 更多