如何以编程方式搜索Google Java API

是否有人知道是否以及如何以编程方式搜索Google-尤其是如果有Java API的话?

回答:

这是一个使用Jsoup作为HTML解析器的启动示例:

String google = "http://www.google.com/search?q=";

String search = "stackoverflow";

String charset = "UTF-8";

String userAgent = "ExampleBot 1.0 (+http://example.com/bot)"; // Change this to your company's name and bot homepage!

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

for (Element link : links) {

String title = link.text();

String url = link.absUrl("href"); // Google returns URLs in format "http://www.google.com/url?q=<url>&sa=U&ei=<someKey>".

url = URLDecoder.decode(url.substring(url.indexOf('=') + 1, url.indexOf('&')), "UTF-8");

if (!url.startsWith("http")) {

continue; // Ads/news/etc.

}

System.out.println("Title: " + title);

System.out.println("URL: " + url);

}

以上是 如何以编程方式搜索Google Java API 的全部内容, 来源链接: utcz.com/qa/412506.html

回到顶部