Home > Java > javaTutorial > Can Java Programmatically Search Google Without a Dedicated API?

Can Java Programmatically Search Google Without a Dedicated API?

Mary-Kate Olsen
Release: 2024-11-23 14:02:13
Original
1051 people have browsed it

Can Java Programmatically Search Google Without a Dedicated API?

How to Search Google Programmatically Using Java API

This article examines the process of searching Google programmatically, specifically focusing on the availability of a Java API for this purpose.

Using Google's Web Search API

Originally, Google offered a public web search API that returned JSON format data. However, this service has since been deprecated. As of November 2010, the best alternative is to query Google's search engine directly using a user agent and then parse the HTML response using a parser such as Jsoup.

Java Implementation

Firstly, let's set up the variables and establish the search URL:

String google = "http://www.google.com/search?q=";
String search = "stackoverflow";
String charset = "UTF-8";
String userAgent = "ExampleBot 1.0 (+http://example.com/bot)"; // Customize for your bot
Copy after login

Next, we use Jsoup to make the HTTP request and parse the HTML:

Elements links = Jsoup.connect(google + URLEncoder.encode(search, charset)).userAgent(userAgent).get().select(".g>.r>a");
Copy after login

Finally, we iterate over the search results and extract the title and URL:

for (Element link : links) {
    String title = link.text();
    String url = link.absUrl("href");
    url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");
    if (!url.startsWith("http")) {
        continue; // Ads/news/etc.
    }
Copy after login

The above is the detailed content of Can Java Programmatically Search Google Without a Dedicated API?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template