这个东西实现了已经有一段时间了,那个时候谷歌还没有退出中国内地呢!而现在呢,谷歌都退了有一些日子了!紧以此纪念一番!

话说谷歌API,我相信很多人应该都知道!不晓得在实际应用中,用的人多不多(我说的不是Web方面的)。谷歌API提供了很多接口,但是貌似唯独没有提供对Delphi的接口(我们Delphi程序员果然很尴尬啊,很多类库,都没有我们的份,都需要自己来实现)。而我又需要这么个东西,于是,我就写了这么个东西,完全基于搜索API的封装!用来实现在自己的软件中实现搜索的目的!

谷歌的搜索API的详细资料在:

http://code.google.com/intl/zh-CN/apis/ajaxsearch/documentation/reference.html#_class_GSearch

有兴趣的,可以自行参考一下!因为这个资料已经说的很详细了,所以我也就不多费口舌了,直接上代码

代码:

代码

{Google搜索API
参考资料:
http://code.google.com/intl/zh-CN/apis/ajaxsearch/documentation/reference.html#_class_GSearch
作者:不得闲 2010-4-1
}
unit DxGoogleSearchApi; interface
uses Classes,SysUtils,msxml,uLkJSON,Variants; type
//搜索类型 Web搜索 本地搜索 视频搜索 博客 新闻 书籍 图片 专利搜索
TDxSearchType = (Sh_Web,Sh_Local,Sh_Video,Sh_Blog,Sh_News,Sh_Book,Sh_Image,Sh_patent); //搜索返回的结果
TDxSearchRecord = class
private
RetList: TStringList;
function GetFieldCount: Integer;
function GetFields(index: Integer): string;
function GetValues(index: Integer): string;
public
constructor Create;
procedure FromJsonObj(JsonObj: TlkJSONobject);
destructor Destroy;override;
property FieldCount: Integer read GetFieldCount;
property Fields[index: Integer]: string read GetFields;
property Values[index: Integer]: string read GetValues;
function FieldByName(FieldName: string): string;
end; TDxSearchRecords = class
private
List: TList;
FSearchType: TDxSearchType;
function GetCount: Integer;
function GetRecords(index: Integer): TDxSearchRecord;
public
procedure Clear;
constructor Create;
property SearchType: TDxSearchType read FSearchType;
destructor Destroy;override;
property Count: Integer read GetCount;
property Records[index: Integer]: TDxSearchRecord read GetRecords;
end; //搜索API
TDxGoogleSearch = class
private
FSearchType: TDxSearchType;
FBigSearchSize: Boolean;
FSearchStart: Integer;
FVersion: string;
HttpReq: IXMLHttpRequest;
FRecords: TDxSearchRecords;
Pages: array of Integer;
FCurSearchInfo: string;
ClearOld: Boolean;
FCurPageIndex: Integer;
function GetPageCount: Integer;
public
constructor Create;
destructor Destroy;override;
procedure Search(SearchInfo: string);
property CurPageIndex: Integer read FCurPageIndex;
function NextSearch: Boolean;//搜索下一个页
property PageCount: Integer read GetPageCount;
property Records: TDxSearchRecords read FRecords;
property BigSearchSize: Boolean read FBigSearchSize write FBigSearchSize default true;//rsz参数
property SearchStart: Integer read FSearchStart write FSearchStart default 0;//搜索开始的位置,start参数
property Version: string read FVersion write FVersion;
property SearchType: TDxSearchType read FSearchType write FSearchType default Sh_Web;//搜索类型
end;
implementation type
TBytes = array of Byte; function BytesOf(const Val: AnsiString): TBytes;
var
Len: Integer;
begin
Len := Length(Val);
SetLength(Result, Len);
Move(Val[1], Result[0], Len);
end; function ToUTF8Encode(str: string): string;
var
b: Byte;
begin
for b in BytesOf(UTF8Encode(str)) do
Result := Format('%s%s%.2x', [Result, '%', b]);
end; { TDxGoogleSearch } constructor TDxGoogleSearch.Create;
begin
HttpReq := CoXMLHTTPRequest.Create;
ClearOld := True;
FRecords := TDxSearchRecords.Create;
FVersion := '1.0';
FSearchType := Sh_Web;
FBigSearchSize := True;
FSearchStart := 0;
end; destructor TDxGoogleSearch.Destroy;
begin
HttpReq := nil;
SetLength(Pages,0);
FRecords.Free;
inherited;
end; function TDxGoogleSearch.GetPageCount: Integer;
begin
Result := High(Pages) + 1;
end; function TDxGoogleSearch.NextSearch: Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to High(Pages) do
begin
if Pages[i] = FSearchStart then
begin
if i + 1 <= High(Pages) then
begin
FSearchStart := Pages[i + 1];
Result := True;
end;
Break;
end;
end;
if Result then
Search(FCurSearchInfo);
end; procedure TDxGoogleSearch.Search(SearchInfo: string);
const
BaseUrl = 'http://ajax.googleapis.com/ajax/services/search/';
var
Url: string;
Json: TlkJsonObject;
ChildJson,tmpJson: TlkJSONbase;
SRecord: TDxSearchRecord;
procedure OnSearch;
var
i: Integer;
begin
Url := Url + '&start='+inttostr(FSearchStart);
HttpReq.open('Get', Url, False, EmptyParam, EmptyParam);
HttpReq.send(EmptyParam);//开始搜索
Url := HttpReq.responseText;
Json := Tlkjson.ParseText(url) as TlkJSONobject;
ChildJson := Json.Field['responseData'];
if ChildJson.SelfType = jsObject then
begin
ChildJson := ChildJson.Field['results'];
if ChildJson.SelfType = jsList then
begin
for i := 0 to ChildJson.Count - 1 do
begin
tmpJson := ChildJson.Child[i];
SRecord := TDxSearchRecord.Create;
SRecord.FromJsonObj(tmpJson as TlkJSONobject);
FRecords.List.Add(SRecord);
end;
end;
if ClearOld or (Length(Pages) = 0) then
begin
//查看分页情况,获得分页情况
ChildJson := Json.Field['responseData'].Field['cursor'].Field['pages'];
if ChildJson.SelfType = jsList then
begin
SetLength(Pages,ChildJson.Count);
for i := 0 to ChildJson.Count - 1 do
begin
tmpJson := ChildJson.Child[i];
Pages[i] := StrToInt(VarToStr(tmpJson.Field['start'].Value));
end;
end;
ChildJson := Json.Field['responseData'].Field['cursor'];
FCurPageIndex := strtoint(vartostr(ChildJson.Field['currentPageIndex'].Value));
end
else
begin
ChildJson := Json.Field['responseData'].Field['cursor'];
FCurPageIndex := strtoint(vartostr(ChildJson.Field['currentPageIndex'].Value));
end;
end;
Json.Free;
end;
begin
FCurSearchInfo := SearchInfo;
case FSearchType of
Sh_Web: Url := BaseUrl + 'web?v='+FVersion+'&q=';
Sh_Local: Url := BaseUrl + 'local?v='+FVersion+'&q=';
Sh_Video: Url := BaseUrl + 'video?v='+FVersion+'&q=';
Sh_Blog: Url := BaseUrl + 'blogs?v='+FVersion+'&q=';
Sh_News: Url := BaseUrl + 'news?v='+FVersion+'&q=';
Sh_Book: Url := BaseUrl + 'books?v='+FVersion+'&q=';
Sh_Image: Url := BaseUrl + 'images?v='+FVersion+'&q=';
Sh_patent: Url := BaseUrl + 'patent?v='+FVersion+'&q=';
else Url := '';
end;
if Url <> '' then
begin
FRecords.FSearchType := FSearchType;
if ClearOld then
FRecords.Clear;
Url := Url + ToUTF8Encode(SearchInfo);
if FBigSearchSize then
Url := Url + '&rsz=large'
else Url := Url + '&rsz=small';
if FSearchStart < 0 then
begin
//搜索返回所有结果
ClearOld := False;
FSearchStart := 0;
OnSearch;
while NextSearch do;//搜索下一个
end
else
begin
OnSearch;
end;
end;
end; { TDxSearchRecord } constructor TDxSearchRecord.Create;
begin
RetList := TStringList.Create;
end; destructor TDxSearchRecord.Destroy;
begin
RetList.Free;
inherited;
end; function TDxSearchRecord.FieldByName(FieldName: string): string;
var
index: Integer;
begin
index := RetList.IndexOfName(FieldName);
if (index > -1) and (index < FieldCount) then
Result := RetList.ValueFromIndex[index]
else Result := '';
end; procedure TDxSearchRecord.FromJsonObj(JsonObj: TlkJsonObject);
var
i: Integer;
str: String;
begin
RetList.Clear;
for i := 0 to JsonObj.Count - 1 do
begin
str := JsonObj.NameOf[i];
str := str + '=' + VarToStr(JsonObj.FieldByIndex[i].Value);
RetList.Add(str);
end;
end; function TDxSearchRecord.GetFieldCount: Integer;
begin
Result := RetList.Count;
end; function TDxSearchRecord.GetFields(index: Integer): string;
begin
if (index > -1) and (index < FieldCount) then
Result := RetList.Names[index]
else Result := '';
end; function TDxSearchRecord.GetValues(index: Integer): string;
begin
if (index > -1) and (index < FieldCount) then
Result := RetList.ValueFromIndex[index]
else Result := '';
end; { TDxSearchRecords } procedure TDxSearchRecords.Clear;
begin
while List.Count > 0 do
begin
TDxSearchRecord(List[List.Count - 1]).Free;
List.Delete(List.Count - 1);
end;
end; constructor TDxSearchRecords.Create;
begin
List := TList.Create;
FSearchType := Sh_Web;
end; destructor TDxSearchRecords.Destroy;
begin
clear;
List.Free;
inherited;
end; function TDxSearchRecords.GetCount: Integer;
begin
Result := List.Count;
end; function TDxSearchRecords.GetRecords(index: Integer): TDxSearchRecord;
begin
if (index > -1) and (index < Count) then
Result := List[index]
else Result := nil;
end; end.

Google的搜索API的Delphi封装的更多相关文章

  1. 使用Javascript从Google Places搜索api获取纬度和经度

    如何使用谷歌地图搜索框api从搜索到的位置获取经度和纬度. 我使用与谷歌演示相同的代码 – https://developers.google.com/maps/documentation/javas ...

  2. 通过Google Custom Search API 进行站内搜索

    今天突然想把博客的搜索改为google的站内搜索,印象中google adsense中好像提高这个站内搜索的代码,但苦逼的是google adsense帐号一直审核不通过,所以只能通过google c ...

  3. Google地图接口API之申请免费API Key(一)

    使用谷歌地图API V3创建交互式地图,首先需要拥有一个免费的 Google 地图 API key. 如果想调用Google地图的接口,首先需要拥有一个免费的 Google 地图 API key.想要 ...

  4. google map android api v2

    我在这主要列举几个需要注意的问题: 1.需要注意使用的api版本的问题,例如google map android api v1就和v2差别很大,包括申请key方面,所以在搜索资料的时候一定注意版本问题 ...

  5. ElasticSearch查询 第一篇:搜索API

    <ElasticSearch查询>目录导航: ElasticSearch查询 第一篇:搜索API ElasticSearch查询 第二篇:文档更新 ElasticSearch查询 第三篇: ...

  6. [转]MBTiles 离线地图演示 - 基于 Google Maps JavaScript API v3 + SQLite

    MBTiles 是一种地图瓦片存储的数据规范,它使用SQLite数据库,可大大提高海量地图瓦片的读取速度,比通过瓦片文件方式的读取要快很多,适用于Android.IPhone等智能手机的离线地图存储. ...

  7. 如何使用google地图的api(整理)

    如何使用google地图的api(整理) 一.总结 一句话总结:直接用script标签引google地图api即可. 1.如何使用google地图的api? 页面引用javascript文件<s ...

  8. google批量搜索实现方式

    本文主要记录一下最近所做的关于Google批量搜索的实现方式. 搜索目的: 获取关键词在某个域名下对应的Google搜索结果数 搜索方式: 关键词+inurl 例如:"爬虫" in ...

  9. 一个扩展搜索API的优化过程

    概述 API 是一个服务的门面,就像衣装是人的形象一样. 优雅的 API 设计,能让业务方使用起来倍儿爽,提升开发效率,降低维护成本:糟糕的 API 设计,则让业务方遭心,陷入混沌. 本文将展示一个扩 ...

随机推荐

  1. GitHub 网站汉化

    居然是一个中文Github网站!该不会是个假的吧? 2018-09-03 17:30 前几天分享了一篇文章——3个搜索技巧!在 GitHub上快速找到实用资源!眼尖心细的读者发现了文中的Github网 ...

  2. 1003 Emergency (25分) 求最短路径的数量

    1003 Emergency (25分)   As an emergency rescue team leader of a city, you are given a special map of ...

  3. Session共享解决方案

    使用nginx做的负载均衡添加一个ip_hash配置 一.开两个Tomcat写测试程序 @WebServlet("/nginxSessionServlet") public cla ...

  4. SpringCloud入门——(1)创建Eureka项目

    Eureka是Spring Cloud Netflix微服务套件中的一部分,可以与Springboot构建的微服务很容易的整合起来.Eureka包含了服务器端和客户端组件.服务器端,也被称作是服务注册 ...

  5. RIFF

    RIFF全称为资源互换文件格式(Resources Interchange File Format),是Windows下大部分多媒体文件遵循的一种文件结构. RIFF文件所包含的数据类型由该文件的扩展 ...

  6. Qt 调用本地浏览器打开URL

    点击Qt某些控件,查找本地浏览器打开前端传递的URL. 方法一:直接写死本地浏览器地址 QString programAdress = "C:\Program Files (x86)\Goo ...

  7. 「AHOI2008」紧急集合/聚会

    题目描述 这次也是很长的题面啊\(qwq\) 题目大意如下: 给定一棵\(N\)个节点的树以及\(M\)次询问,每次询问给出\(x,\ y,\ z\)三个节点,程序需要在树上找一个点\(p\) 使得\ ...

  8. 设计模式课程 设计模式精讲 18-2 迭代器模式coding

    1 代码演练 1.1 代码演练1(迭代器模式演练) 1.2 代码使用场景 1 代码演练 1.1 代码演练1(迭代器模式演练) 需求: 课程管理:需要实现课程可进行增添,删除,并能够打印出课程列表. u ...

  9. VS2010解决闪退的方法

    VS2010解决闪退的原因 前言 在利用vs2010编译器进行编写程序的时候程序结果无法看到,针对上述问题有如下两个解决方法: 方法1. 在程序结束之前(return之前)加  system(&quo ...

  10. 分享Linux CentOS7 VMware 系统目录结构、 ls命令 、文件类型、alias命令——笔记

    一. 系统目录结构 生成目录树结构: tree -a 显示所有 tree -d 仅显示目录 tree -L n n代表数字..表示要显示几层... tree -f 显示完整路径.. yum insta ...