Согласно источнику HTML http://google.com кнопка "Мне повезет" имеет название btnI
:
<input value="I'm Feeling Lucky" name="btnI" type="submit" onclick="..." />
Итак, достаточно добавить параметр btnI
в строку запроса (значение не имеет значения):
http://www.google.com/search?hl=en&btnI=1&q=your+search+term
Итак, этот Jsoup должен сделать:
String url = "http://www.google.com/search?hl=en&btnI=1&q=balusc";
Document document = Jsoup.connect(url).get();
System.out.println(document.title());
Однако это привело к ошибке 403 (Запрещено) .
Exception in thread "main" java.io.IOException: 403 error loading URL http://www.google.com/search?hl=en&btnI=1&q=balusc
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:387)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:364)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:143)
at org.jsoup.helper.HttpConnection.get(HttpConnection.java:132)
at test.Test.main(Test.java:17)
Возможно, Google понюхал пользовательский агент и обнаружил, что это Java. Итак, я изменил это:
String url = "http://www.google.com/search?hl=en&btnI=1&q=balusc";
Document document = Jsoup.connect(url).userAgent("Mozilla").get();
System.out.println(document.title());
Это дает (как и ожидалось):
Код BalusC
403, однако, указывает на то, что Google не обязательно доволен такими ботами. Если вы делаете это слишком часто, вы можете (временно) заблокировать IP-адрес.