gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 10:38:28 24 4
gpt4 key购买 nike

我有一个 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/

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