Jsoup可以模拟按下按钮吗?
您可以使用Jsoup向Google提交搜索,但不是通过“Google搜索”发送您的请求,而是使用“我感觉很幸运”吗? 我想捕获将返回的网站的名称。
我看到很多提交表单的例子,但从来没有办法指定一个特定的按钮来执行搜索或表单提交。
如果Jsoup不起作用,那会是什么?
根据http://google.com的HTML源代码,“我很幸运”按钮的名称为btnI
:
因此,只需将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)
也许谷歌正在嗅探用户代理并发现它是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表明谷歌并不一定对这样的机器人感到满意。 当您经常这样做时,您可能会(暂时)获得IP禁用。
我会尝试使用HtmlUnit来浏览网站,然后使用JSOUP进行搜索
是的,如果您能够弄清楚Google搜索查询是如何制作的,那么它可以。 但谷歌不允许这样做,即使你成功了。 您应该使用他们的官方API来进行自动搜索查询。
http://code.google.com/intl/en-US/apis/customsearch/v1/overview.html