如何在java中删除503个异常

时间:2016-01-01 01:16:53

标签: java http-status-code-503

我有一个CSV形式的名单列表,我可以使用java搜索这些名称。但我面临的问题是,当我最初运行代码时,我能够搜索查询但在代码中间代码开始抛出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();
       }     
}       

}

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

1 个答案:

答案 0 :(得分:1)

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

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