免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3857 | 回复: 0
打印 上一主题 下一主题

Google API 创建搜索 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-07-25 09:47 |只看该作者 |倒序浏览
需要的类  

要通过 Java 使用 Google API,请确保适当的类都能找到。Google 为方便起见已经将这些类捆绑到了 googleapi.jar 文件中,这个文件作为分发包的一部分被包含。这个文件包括:

用于 SOAP 类的 Java 包装器类,在 package com.google.soap.search 中。
Java Activation Framework 1.0.1,通常您可以在 activation.jar 中找到。
JavaMail TM API,通常您可以在 mailapi.jar 中找到。
Apache SOAP 2.2 工具箱,通常您可以在 apache-soap-22.jar 中找到。
Apache Crimson 1.1.3,通常您可以在 crimson.jar 中找到。(请注意,这是 Crimson,而“不是”Xerces。这两者处理 XML 的方式相似,但不完全相同。)
请注意在 googleapi.jar 中不包括 Java API for XML Messaging(JAXM),但使用 Google API 不一定需要。


您将从此处开始实际创建一个简单的搜索,执行它,然后研究结果。示例中会展示一些简单的应用程序,它们的结果会输出到命令行;您怎么处理最终数据是您的事。

import com.google.soap.search.GoogleSearch;
         
public class GoogleSearchTutorial {
         
     public static void main (String[] args) {
      
         String searchTerm = args[0];
         
         GoogleSearch search = new GoogleSearch();
         
         search.setKey("00000000000000000000000000000000");
         search.setQueryString(searchTerm);
         
     }
         
}
在这个示例中主要的对象是 GoogleSearch。使用的时候,你要设置参数,然后实际执行搜索,就会有结果返回。

请注意,setKey() 让您将个人的授权码发到应用程序。请用您注册 Google 帐户时收到的授权码替换这些零。

除此之外,执行搜索所需要的就只有搜索项本身了。我会在构造搜索中更多的说到构建搜索,但目前就认为您可以搜索任何词或词组。

您一设置好授权码和搜索项,就为实际执行搜索做好了准备。


实际上,执行搜索相当方便:

import com.google.soap.search.GoogleSearch;
import com.google.soap.search.GoogleSearchResult;  
import com.google.soap.search.GoogleSearchFault;      
         
public class GoogleSearchTutorial {
         
     public static void main (String[] args) {
      
         String searchTerm = args[0];
         
         try {      
         
            GoogleSearch search = new GoogleSearch();
         
            search.setKey("00000000000000000000000000000000");
            search.setQueryString(searchTerm);
         
            GoogleSearchResult result = search.doSearch();  

          int numResults = result.getEstimatedTotalResultsCount();  
      
            if ( result.getEstimateIsExact() ){
               System.out.println("Number of results: "+numResults);
            } else {
               System.out.println("Estimated number of results:"+numResults);
            }
   } catch (GoogleSearchFault gsf) {  
        System.out.println("Google Search Fault: "+gsf.getMessage());  
   }      
         
     }
         
}
doSearch() 方法依据前一屏中设定的查询条件进行搜索,返回对象 GoogleSearchResult,可以使用它携带的方法来访问数据。特别有用的一点信息是结果的个数以及该数字是否准确。

例如,如果编译和运行这个应用程序时使用了命令行参数:"science fiction"(包括引号),它将返回如下文本:

Estimated number of results: 1790000
在向 Web 服务请求信息时,任何事情都可能出错,Google API 包括了 GoogleSearchFault 类,它充当可能出现的任何异常的包装器。


检索结果本身并不难。GoogleSearchResult 对象就提供了一种查找结果本身的简单方式:

...
            search.setKey("00000000000000000000000000000000");
            search.setQueryString(searchTerm);
         
            GoogleSearchResult result = search.doSearch();
            System.out.println( result.toString() );
            
         } catch (GoogleSearchFault gsf) {
...
被覆盖掉的 toString() 方法提供了一种很方便的方法来查看结果。例如,同样搜索 "science fiction" 将返回如下的结果:

[
    URL    = "http://www.sfsite.com/home.htm"
    Title = "The SF Site: The Best in <b>Science</b>
<b>Fiction</b> and Fantasy"
    Snippet = " <b>...</b> Magazine of Fantasy &
<b>Science</b> <b>Fiction</b>
, April 2002 The Magazine of Fantasy & <b>Science</b
><br> <b>Fiction</b>, April 2002 reviewed by David Soyka Charles
Coleman Finlay cover <b>...</b>     "
    Directory Category = {SE="", FVN=""}
    Directory Title = ""
    Summary = ""
    Cached Size = "36k"
    Related information present = true
    Host Name = ""
    ],
         
    [
    URL    = "http://www.asimovs.com/"
    Title = "Asimov's <b>Science</b> <b>Fiction</b>"
    Snippet = "Asimov's SF T-shirts Quantities are limited, so buy your own<br> As
imov's <b>Science</b> <b>Fiction</b> t-shirt while supplies last!
<b>...</b>     "
    Directory Category = {SE="", FVN="Top/Arts/Literature/Genres/
     Science_Fiction/Magazines_and_E-zines"}
    Directory Title = "Asimov's <b>Science</b> <b>Fiction</b> "
    Summary = "Essays by Isaac Asimov, stories, book and net reviews,
     readers' forum and chats, writers guidelines,... "
    Cached Size = "31k"
    Related information present = true
    Host Name = ""
    ],
         
    [
    URL    = "http://worldcon.org/"
    Title = "World <b>Science</b> <b>Fiction</b> Society /
Worldcon [Official]"
    Snippet = "World <b>Science</b> <b>Fiction</b> Society World
<b>Science</b> <b
>Fiction</b> Convention.<br> [updated 7 november 2001]. Upcoming Worldcons. <b>.
..</b>     "
    Directory Category = {SE="",
FVN="Top/Arts/Genres/Science_Fiction_and_Fantasy/
Fandom/Conventions_and_Organizations/WorldCon"}
    Directory Title = "World <b>Science</b> <b>Fiction</b> Society /
WorldCon "
    Summary = "Website with some basic information, schedule of upcoming WorldCons
, and links to the websites of... "
    Cached Size = "10k"
    Related information present = true
    Host Name = ""
    ],
...
      
      
     
以上这个不完整的清单中展示了每个结果的一些可用信息。虽然我想从检索结果集开始提取(以及控制)一些信息,但首先要看一下影响 Google 搜索本身的一些因素。

构造搜索


找什么都能找到的关键是知道问些什么。这对于 Google 同样正确。API 搜索使用的算法和 Web 搜索的一模一样,因此这将有助于理解。

当您要找的词不止一个时,Google 将只返回那些包含所有搜索项的页面,但不一定是按所请求的顺序。例如,搜索项

science fiction
将返回包含“science”和“fiction”这两个词的所有页面,只要它们在文中某处出现过,不管是在正文、标题还是 URL 中,也不管它们是不是作为整体出现。硬要它们作为整体出现,则搜索项必须是

"science fiction"
引号使 Google 只去搜索作为词组的搜索项。要搜索包含其中一个词的页面,请使用

science OR fiction
注意,OR 必须大写,否则它将会被当作停止词。

停止词和其它搜索修饰词  


停止词是指从查询中略去的常见词。它们包括 the、and、a、or 以及 how,等等。硬要在查询中包括停止词,请在前面冠以加号(+),如

"+the science +of fiction"

注意,只是用引号把词组括住还不足以总能使常见词被包括在内。

同样,您可以使用减号(-)排除一些项,如

science -fiction

它将只返回有关科学方面的结果。

Google 还查询将确定在哪里寻找搜索项的许多不同修饰词。这些修饰词包括:

intitle:
allintitle:
inurl:
allinurl:
allintext:
allinlinks:
filetype:
link:
daterange:
这些修饰词可以与其它搜索词结合起来以筛选结果集。例如,查询

"science fiction" daterange:2452365-2452395
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP