Google的搜索API的Delphi封装

简介:

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

  话说谷歌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 .
复制代码

 


本文转自 不得闲 博客园博客,原文链接:http://www.cnblogs.com/DxSoft/archive/2010/04/10/1708964.html   ,如需转载请自行联系原作者


相关文章
|
14天前
|
人工智能 前端开发 API
Gemini Coder:基于 Google Gemini API 的开源 Web 应用生成工具,支持实时编辑和预览
Gemini Coder 是一款基于 Google Gemini API 的 AI 应用生成工具,支持通过文本描述快速生成代码,并提供实时代码编辑和预览功能,简化开发流程。
100 38
Gemini Coder:基于 Google Gemini API 的开源 Web 应用生成工具,支持实时编辑和预览
|
4月前
|
缓存 测试技术 API
API的封装步骤流程
API封装流程是一个系统化的过程,旨在将内部功能转化为可复用的接口供外部调用。流程包括明确需求、设计接口、选择技术和工具、编写代码、测试、文档编写及部署维护。具体步骤为确定业务功能、数据来源;设计URL、请求方式、参数及响应格式;选择开发语言、框架和数据库技术;实现数据连接、业务逻辑、错误处理;进行功能、性能测试;编写详细文档;部署并持续维护。通过这些步骤,确保API稳定可靠,提高性能。
|
15天前
|
缓存 API 开发者
京东按图搜索商品(拍立淘)API接口系列(京东API)
京东按图搜索商品(拍立淘)API 接口(.jd.item_search_img)通过上传图片搜索京东平台上的相似商品,基于图像识别技术提供便捷的商品搜索方式。适用于电商平台展示、比价等场景。响应参数包括公共参数、商品信息及搜索结果相关参数,方便分页展示和了解整体搜索规模。Python 请求示例展示了如何使用该接口进行图片搜索。
68 15
|
16天前
|
JSON 数据挖掘 API
唯品会按关键字搜索 VIP 商品 API 接口的开发应用与收益
在电商蓬勃发展的今天,精准的商品搜索功能至关重要。唯品会的按关键字搜索VIP商品API接口通过高效、精准的检索,提升了用户购物体验和商家销售业绩。该接口基于RESTful架构,采用JSON格式交互,支持唯品会APP内搜索、第三方平台合作及数据分析等场景,显著提升用户活跃度与忠诚度,拓展销售渠道,增加收入,并挖掘数据驱动的商业价值,助力唯品会持续发展。
27 4
|
26天前
|
监控 搜索推荐 API
京东按图搜索京东商品(拍立淘)API接口的开发、应用与收益
京东通过开放商品详情API接口,尤其是按图搜索(拍立淘)API,为开发者、企业和商家提供了创新空间和数据支持。该API基于图像识别技术,允许用户上传图片搜索相似商品,提升购物体验和平台竞争力。开发流程包括注册账号、获取密钥、准备图片、调用API并解析结果。应用场景涵盖电商平台优化、竞品分析、个性化推荐等,为企业带来显著收益,如增加销售额、提高利润空间和优化用户体验。未来,随着数字化转型的深入,该API的应用前景将更加广阔。
81 1
|
1月前
|
存储 人工智能 API
(Elasticsearch)使用阿里云 infererence API 及 semantic text 进行向量搜索
本文展示了如何使用阿里云 infererence API 及 semantic text 进行向量搜索。
|
1月前
|
机器学习/深度学习 API 数据库
淘宝拍立淘按图搜索商品API接口详解
拍立淘按图搜索商品API接口提供了一种通过上传商品图片来搜索相似或相同商品的功能。用户只需上传一张商品图片,系统通过图像识别技术对该图片进行分析和处理,提取出商品的特征信息,并在商品数据库中进行匹配搜索,最终返回与上传图片相似或相同的商品列表。这一功能广泛应用于电商平台、购物应用以及图像搜索等领域,极大地提升了用户的购物体验。
|
1月前
|
机器学习/深度学习 搜索推荐 API
淘宝/天猫按图搜索(拍立淘)API的深度解析与应用实践
在数字化时代,电商行业迅速发展,个性化、便捷性和高效性成为消费者新需求。淘宝/天猫推出的拍立淘API,利用图像识别技术,提供精准的购物搜索体验。本文深入探讨其原理、优势、应用场景及实现方法,助力电商技术和用户体验提升。
|
2月前
|
XML JSON API
[1688一件代发]API接口关键词搜索(1688.item_search)
1688 一件代发的关键词搜索 API 接口,可快速从平台海量商品库中查找相关信息。主要参数包括:q(搜索关键字)、start_price 和 end_price(价格区间)、page(页码)、page_size(每页显示数量)、sort(排序方式)及 filter(额外过滤条件)。此接口适用于需要精准定位商品的开发者和商家。
|
3月前
|
人工智能 搜索推荐 API
用于企业AI搜索的Bocha Web Search API,给LLM提供联网搜索能力和长文本上下文
博查Web Search API是由博查提供的企业级互联网网页搜索API接口,允许开发者通过编程访问博查搜索引擎的搜索结果和相关信息,实现在应用程序或网站中集成搜索功能。该API支持近亿级网页内容搜索,适用于各类AI应用、RAG应用和AI Agent智能体的开发,解决数据安全、价格高昂和内容合规等问题。通过注册博查开发者账户、获取API KEY并调用API,开发者可以轻松集成搜索功能。

热门文章

最新文章