您现在的位置:首页 >> 网络通讯 >> 网络通讯 >> 内容

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

时间:2011/9/3 15:12:21 点击:

  核心提示:Bing API 开发地址:http://cn.bing.com/developers示例一:拼写检查function DoSpellCheck(text: string): string; varx...

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; 

 

作者:站长 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号