翻翻过去那场雪
要使用api搜索google,您应该使用Google自定义搜索,抓取网页是不允许在java中,您可以使用CustomSearchAPI客户端Java库maven依赖项是:<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-customsearch</artifactId>
<version>v1-rev57-1.23.0</version></dependency>使用GoogleCustomSearchAPI客户端库进行示例代码搜索public static void main(String[] args) throws GeneralSecurityException, IOException {
String searchQuery = "test"; //The query to search
String cx = "002845322276752338984:vxqzfa86nqc"; //Your search engine
//Instance Customsearch
Customsearch cs = new Customsearch.Builder(GoogleNetHttpTransport.newTrustedTransport(), JacksonFactory.getDefaultInstance(), null)
.setApplicationName("MyApplication")
.setGoogleClientRequestInitializer(new CustomsearchRequestInitializer("your api key"))
.build();
//Set search parameter
Customsearch.Cse.List list = cs.cse().list(searchQuery).setCx(cx);
//Execute search
Search result = list.execute();
if (result.getItems()!=null){
for (Result ri : result.getItems()) {
//Get title, link, body etc. from search
System.out.println(ri.getTitle() + ", " + ri.getLink());
}
}}如你所见,你需要请求API密钥和设置自己的搜索引擎id,cx.请注意,在设置CX期间,您可以通过在基本选项卡设置上选择“搜索整个网站”来搜索整个网站,但是结果将与普通浏览器Google搜索不完全相同。目前(答案的日期),你每天得到100个API调用免费,然后谷歌喜欢分享你的利润。