Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的谷歌搜索_Java_Search - Fatal编程技术网

Java中的谷歌搜索

Java中的谷歌搜索,java,search,Java,Search,这个程序读取并获取搜索查询的文本文件,用它们查询Google,并将所有链接输出到另一个文件。该程序可以处理几百个查询,但突然工作并报告错误 我将编辑这篇文章,并在不久的将来发布从我的程序的哪行返回的错误 你知道会发生什么吗 import java.io.*; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.regex.Matcher; import j

这个程序读取并获取搜索查询的文本文件,用它们查询Google,并将所有链接输出到另一个文件。该程序可以处理几百个查询,但突然工作并报告错误

我将编辑这篇文章,并在不久的将来发布从我的程序的哪行返回的错误

你知道会发生什么吗

import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.Scanner;

public class GoogleSearcher {
  public static void main(String [] args) throws Exception {
    Scanner in = new Scanner (System.in);
    System.out.println("Input list of queries to search:");
    String loc = in.nextLine();
    loc = loc.replace("\\", "");
    System.out.println("Where to write file?");
    String writeLoc = in.nextLine();
    writeLoc = writeLoc.replace("\\", " ");
    FileInputStream fstream = new FileInputStream(loc);
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    String line;
    PrintWriter pw = new PrintWriter(new FileWriter(writeLoc + "Google Search Results.txt"));
    while ((line = br.readLine()) != null) {
      System.out.println("Searching: \"" + line + "\"");
      ArrayList<String> t = googleSearch(line);
      if (t != null){
        for (int a = 0; a < t.size(); a++){
          pw.write(t.get(a) + System.lineSeparator());
        }
      }
    }
    br.close();
    pw.close();
  }
  public static ArrayList<String> googleSearch(String search) throws Exception {
    try {
      String query = "https://www.google.com/search?q=" + search.replace(" ", "%20");
      String page = getSearchContent(query);
      ArrayList<String> links = parseLinks(page);
      return formatLinks(links);
    } catch (Exception e) { 
      e.printStackTrace();
      System.out.println("Error... Trying next search");
      return null;
    } 
  }
  public static ArrayList<String> formatLinks(ArrayList a){
    ArrayList<String> formatted = new ArrayList<String>();
    for (int i = 0; i < a.size(); i++){
      String t = (String)a.get(i);
      t = t.replace("%3F", "?");
      t = t.replace("%3D", "=");
      formatted.add(t);
    }
    return formatted;
  }
  public static String getString(InputStream is) {
    StringBuilder sb = new StringBuilder();
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
    String line;
    try {
      while ((line = br.readLine()) != null) {
        sb.append(line);
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (br != null) {
        try {
          br.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
    return sb.toString();
  }
  public static String getSearchContent(String path) throws Exception {
    final String agent = "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
    URL url = new URL(path);
    final URLConnection connection = url.openConnection();
    connection.setRequestProperty("User-Agent", agent);
    final InputStream stream = connection.getInputStream();
    return getString(stream);
  }
  public static ArrayList<String> parseLinks(final String html) throws Exception {
    ArrayList<String> result = new ArrayList<String>();
    String pattern1 = "<h3 class=\"r\"><a href=\"/url?q=";
    String pattern2 = "\">";
    Pattern p = Pattern.compile(Pattern.quote(pattern1) + "(.*?)" + Pattern.quote(pattern2));
    Matcher m = p.matcher(html);
    while (m.find()) {
      String domainName = m.group(0).trim();
      // remove unwanted text
      domainName = domainName.substring(domainName.indexOf("/url?q=") + 7);
      domainName = domainName.substring(0, domainName.indexOf("&amp;"));
      result.add(domainName);
    }
    return result;
  }
}

好的,在运行了几轮程序之后,我得到了以下错误

Error... Trying next search
Searching: "autoradiograph"
java.io.IOException: Server returned HTTP response code: 503 for URL: https://ipv4.google.com/sorry/index?continue=https://www.google.com/search%3Fq%3Daustria&q=EgTLe7ahGOKSrcMFIhkA8aeDSylzciRE9l0cz9fUg6u2MeGh-muxMgNyY24
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1876)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1474)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)
    at application.GoogleSearcher.getSearchContent(GoogleSearcher.java:90)
    at application.GoogleSearcher.googleSearch(GoogleSearcher.java:45)
    at application.GoogleSearcher.main(GoogleSearcher.java:32)
java.io.IOException: Server returned HTTP response code: 503 for URL: https://ipv4.google.com/sorry/index?continue=https://www.google.com/search%3Fq%3Dautoradiograph&q=EgTLe7ahGOKSrcMFIhkA8aeDS_cQehdQreptc4cInLKEPYpprweeMgNyY24
这种情况正在发生,因为谷歌正在阻止自动搜索以防止对其服务器的攻击

谷歌可能不允许您执行自动搜索。这是一个例子。这是那一页的摘录

自动查询

谷歌的服务条款不允许在没有谷歌事先明确许可的情况下向我们的系统发送任何形式的自动查询。发送自动查询会消耗资源,包括使用任何软件(如WebPosition Gold)向谷歌发送自动查询,以确定网站或网页在谷歌各种查询搜索结果中的排名。除了排名检查,其他类型的未经许可自动访问谷歌也违反了我们的网站管理员指南和服务条款


那是因为它是这样设计的。每当谷歌检测到某种自动软件正在获取其结果时,它就会要求人工验证并显示验证码

来自计算机网络的异常流量

您可能会看到我们的系统检测到来自您的 计算机网络如果它看起来像你网络上的计算机或电话 正在向谷歌发送自动流量

谷歌对自动流量的看法

从机器人、计算机程序、自动服务或搜索刮板发送搜索 使用向谷歌发送搜索的软件来查看网站或网页在谷歌上的排名 看到此消息时要做什么

错误页面很可能显示一个验证码,一个带方框的扭曲单词 在它下面。要继续使用Google,请在 盒这就是为什么我们知道你是人类,而不是机器人。在您键入 如果验证码正确,消息将消失,您可以使用谷歌 再说一遍

如果你想在你的网站上使用谷歌搜索,那么你可以使用谷歌搜索


另请参见:

您收到了什么错误?谷歌可能会阻止这种自动使用他们的服务。是的,有什么错误吗?我试了大约300个单词,看起来运行得很好。你应该捕获/记录所有状态代码和错误,特别是那些从谷歌服务返回的代码和错误。正如@ti7所说,有许多站点跟踪连接/查询的数量和速率,并阻止超过阈值的客户端。通常,当它看到一定数量的请求是在一定的时间段内发出的,所以如果你能弄清楚那是什么,你就可以限制你的代码。你可以让它以一定的速度运行,跟踪谷歌何时停止响应,何时重新开始工作,然后调整速度,并尝试找到一种模式。还有谷歌it:错误是谷歌在一段时间后阻止了自动搜索,我正试图找到一种解决方法,因为我有几千个查询要记录。。实际上,我可能会尝试稍微限制我的代码,我会用谷歌搜索如何做到这一点;您可以使用它,它负责验证码和代理。