delphi中调用必应搜索(Bing)的API函数  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi中调用必应搜索(Bing)的API函数


Bing API 开发地址:http://cn.bing.com/developers
示例一:拼写检查
function DoSpellCheck(text: string): string;
var
xmldoc: TXMLDocument;
inode,mnode,rnode,irnode: IXMLNode;
j: integer;
uri: string;
webcopy: TWebCopy;
begin
Result := '';

webcopy := TWebCopy.Create(application);
try
with webcopy.Items.Add do
begin
url := 'http://api.bing.net/xml.aspx?Appid=' + AppID + '&query='+HTTPEncode(text)+
'&Sources=Spell&Version=2.0&Market=en-us&Options=EnableHighlighting';

TargetFilename := 'response.xml';
Protocol := wpHttp;
end;
webcopy.ShowDialog := false;
webcopy.Execute;
finally
webcopy.Free;
end;

xmldoc := TXMLDocument.Create(application);
try
xmldoc.LoadFromFile('response.xml');

inode := xmldoc.ChildNodes.FindNode('SearchResponse');

if Assigned(inode) then
begin
uri := 'http://schemas.microsoft.com/LiveSearch/2008/04/XML/spell';

mnode := inode.ChildNodes.FindNode('Spell',uri);
if Assigned(mnode) then
begin
rnode := mnode.ChildNodes.FindNode('Results',uri);
if Assigned(rnode) then
begin
irnode := rnode.ChildNodes.FindNode('SpellResult',uri);
if Assigned(irnode) then
Result := irnode.ChildNodes.FindNode('Value',uri).NodeValue;
end;
end;
end;
finally
xmldoc.Free;
end;
end;

begin
// sample call with a forced spelling error
ShowMessage( DoSpellCheck('Mispeling words is a common ocurrence') );
end;
示例二:获取图片链接
function GetImageLink(searchstr: string): string;
var
xmldoc: TXMLDocument;
inode,mnode,rnode,irnode: IXMLNode;
j: integer;
uri: string;
webcopy: TWebCopy;
begin
Result := '';

webcopy := TWebCopy.Create(application);
try
with webcopy.Items.Add do
begin
url := 'http://api.bing.net/xml.aspx?Appid=' + AppID + '&query='+HTTPEncode(searchstr)+'&sources=image&image.count=1';
TargetFilename := 'response.xml';
Protocol := wpHttp;
end;
webcopy.ShowDialog := false;
webcopy.Execute;
finally
webcopy.Free;
end;

xmldoc := TXMLDocument.Create(application);
try
xmldoc.LoadFromFile('response.xml');

inode := xmldoc.ChildNodes.FindNode('SearchResponse');

if Assigned(inode) then
begin
uri := 'http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia';
mnode := inode.ChildNodes.FindNode('Image',uri);
if Assigned(mnode) then
begin
rnode := mnode.ChildNodes.FindNode('Results',uri);
if Assigned(rnode) then
begin
irnode := rnode.ChildNodes.Nodes[0];
if Assigned(irnode) then
Result := irnode.ChildNodes.FindNode('MediaUrl',uri).NodeValue;
end;
end;
end;
finally
xmldoc.Free;
end;
end;

begin
// sample retrieving an image hyperlink and show it on the form using a TWebImage component
imageurl := GetImageLink('mercedes SL gullwing');
caption := imageurl;
Screen.Cursor := crHourGlass;
WebImage1.URL := imageurl;
Screen.Cursor := crDefault;
end;
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.046468019485474 seconds