java - 如何消除java中的503异常

标签 java http-status-code-503

我有一个 CSV 形式的姓名列表,我准备使用 java 在 google 上搜索这些姓名。但我面临的问题是,当我最初运行代码时,我能够搜索查询,但在代码中间,代码开始抛出 503 异常,当我再次运行代码时,它开始抛出 503 异常这是我正在使用的代码。

public class ExtractInformation 
{
  static String firstname,middlename,lastname;  
  public static final int PAGE_NUMBERS = 10;
  public static void readCSV()
  {
      boolean first = true;
      try
      {
          String splitBy = ",";
          BufferedReader br = new BufferedReader(new FileReader("E:\\KOLDump\\names.csv"));
          String line = null;
          String site = null;

          while((line=br.readLine())!=null)
          {
              if(first)
              {
                  first = false;
                  continue;
              }
              String[] b = line.split(splitBy);
              firstname  = b[0];
              middlename = b[1];
              lastname   = b[2];
              String name = null;
              if(middlename == null || middlename.length() == 0)
              {
                   name = firstname+" "+lastname+" OR "+lastname+" "+firstname.charAt(0);
              }
              else
              {
                  name = firstname+" "+lastname+" OR "+lastname+" "+firstname.charAt(0)+" OR "+firstname+" "+middlename.charAt(0)+". "+lastname;
              }
              BufferedReader brs = new BufferedReader(new FileReader("E:\\KOLDump\\site.csv"));
              while((site = brs.readLine()) != null)
              {
                  if(first)
                  {
                      first = false;
                      continue;
                  }
                  String [] s = site.split(splitBy);
                  String siteName = s[0];
                  siteName = (siteName.replace("www.", ""));
                  siteName = (siteName.replace("http://", ""));
                  getDataFromGoogle(name.trim(), siteName.trim());
              }
              brs.close();
          }
          //br.close();

      }
      catch(Exception e)
      {
          System.out.println("unable to read file...some problem in the csv");
      }

  }
  public static void main(String[] args) 
  {
      readCSV();
  }

  private static void getDataFromGoogle(String query,String siteName) 
  {        
       Set<String> result = new HashSet<String>();   
       String request = "http://www.google.co.in/search?q="+query+" "+siteName;       
       try 
       {
            Document doc = Jsoup.connect(request).userAgent("Chrome").timeout(10000).get();
            Element query_results = doc.getElementById("ires");
            Elements gees = query_results.getElementsByClass("g");
            for(Element gee : gees)
            {
                Element h3 = gee.getElementsByTag("h3").get(0);
                String annotation = h3.getElementsByTag("a").get(0).attr("href"); 
                if(annotation.split("q=",2)[1].contains(siteName))
                {
                    System.out.println(annotation.split("q=",2)[1]);
                }
            }

       } 
       catch (IOException e) 
       {
             e.printStackTrace();
       }     
}       

}

任何有关如何从代码中删除此异常的建议都会非常有帮助。

最佳答案

如果稍等一下,503 会消失吗?如果是这样,那么您可能受到了 Google 的速率限制。 https://support.google.com/gsa/answer/2686272?hl=en

您可能需要在请求之间设置某种延迟。

关于java - 如何消除java中的503异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34552878/

相关文章:

java - 减少 Java Lambda 函数部署包中 jar 文件的数量

c# - 使用依赖注入(inject)的现实世界解决方案

http - 降级健康检查的 HTTP 状态码应该是什么?

java - 上下文参数值不是字符串

java - 调用接口(interface)中的私有(private)方法

java - 将 Java 类库中的类替换为自定义版本

php - PHP 中的长时间计算导致 503 错误

node.js - Heroku Node 503

node.js - Node : CORS Fails Occasionally

php - Heroku H18 503 "Request Interrupted"错误