我正在使用此代码使用谷歌对单词列表进行查询,并从谷歌中提取搜索结果的数量.它工作正常,但自从昨晚它在执行200次查询后不断给我这个错误(我猜google标记了我!):线程"main"中的异常java.io.IOException:服务器返回HTTP响应代码:503为URL:http ://www.google.com/sorry/?continue = http://www.google.com/ ...
"红色"只是一个例子.
public class Google {
public static void main(String[] args) throws IOException {
String query = "red";
String urlName = "http://www.google.com/search?q=\""+query+"\"";
URL url = new URL(urlName);
URLConnection conn = url.openConnection();
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (X11; U; Linux x86_64; en-GB; rv:1.8.1.6) Gecko/20070723 Iceweasel/2.0.0.6 (Debian-2.0.0.6-0etch1)");
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
Pattern pattern = Pattern.compile("<div>About (.*?) results</div>"); //<div>About 1,620,000 results</div>
String line;
while ((line = in.readLine()) != null) {
Matcher m = pattern.matcher(line);
if (m.find()) {
System.out.println(m.group(1)); // m.group(1) coresponds to results number: i.e.: 1,620,000
}
}
in.close();
}
Run Code Online (Sandbox Code Playgroud)
}
任何解决方案,建议?
您已被标记为机器人,可能是由于您的查询频率.尝试从不同的IP运行它(在此之前标记为机器人).
无论如何,您应该使用Google搜索API.从网站 https://developers.google.com/custom-search/v1/overview:
免费配额
使用对所有用户免费,每天最多100个查询.
付费使用
如果您未注册结算,则超出免费使用配额的任何使用都将失败.启用结算后,您每天将继续收到100个免费查询.但是,对于所有额外请求,您将按每1000次查询5美元的费率收费,每天最多10,000次查询.如果您需要额外配额,请从控制台请求额外配额.