Java – can I press the jsup simulation button?

You can use jsup to submit a search to Google, but instead of sending your request through "Google search", you can use "I feel lucky"? I want to capture the name of the website that will be returned

I've seen many examples of submitting forms, but there's never been a way to specify a specific button to perform a search or form submission

What happens if jsup doesn't work?

Solution

According to http://google.com The "I feel lucky" button has a name btni:

<input value="I'm Feeling Lucky" name="btnI" type="submit" onclick="..." />

Therefore, simply add the btni parameter to the query string (the value does not matter):

Therefore, this jsup should:

String url = "http://www.google.com/search?hl=en&btnI=1&q=balusc";
Document document = Jsoup.connect(url).get();
System.out.println(document.title());

However, this gives a 403 (Forbidden) error

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)

Maybe Google is sniffing user agents and discovering them as Java So I changed:

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());

This produces (as expected):

However, 403 shows that Google is not necessarily satisfied with such robots When you do this often, you may temporarily (IP) disable IP

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>