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;

相关阅读 >>

Delphi 简单实习窗体靠边隐藏

Delphi 2009 之 tballoonhint

Delphi跨平台的字符串代码标准

Delphi xe 枚举指定目录及子目录下的所有文件

Delphi实现ftp上传与下载

Delphi复制文件时,如何显示进度条

Delphi tstringlist.find

Delphi的dll中获取文件路径

Delphi 字符转16进制、16进制转字符

Delphi 对txt文件的操作

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...