如何以编程方式搜索Google有谁知道是否以及如何通过编程方式搜索Google-尤其是如果有JavaAPI的话?
3 回答

翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
<dependency> <groupId>com.google.apis</groupId> <artifactId>google-api-services-customsearch</artifactId> <version>v1-rev57-1.23.0</version></dependency>
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()); } }}
请注意,在设置CX期间,您可以通过在基本选项卡设置上选择“搜索整个网站”来搜索整个网站,但是结果将与普通浏览器Google搜索不完全相同。
添加回答
举报
0/150
提交
取消