码迷,mamicode.com
首页 > 其他好文 > 详细

XE3随笔17:实例 - 模拟 Google 搜索

时间:2015-08-13 11:21:37      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

本例测试效果图:

技术分享



代码文件:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses MsXML, SuperObject;

//这是将字符串转换成 UTF 编码的函数, 这里用于 Google 的搜索地址
function ToUTF8Encode(str: string): string;
var
  b: Byte;
begin
  for b in BytesOf(UTF8Encode(str)) do
    Result := Format(‘%s%s%.2x‘, [Result, ‘%‘, b]);
end;

//这是给 ISuperObject 准备的方法
procedure Proc(const This, Params: ISuperObject; var Result: ISuperObject);
var
  jo: ISuperObject;
begin
  Form1.Memo1.Clear;
  for jo in Params[‘responseData.results‘] do with Form1.Memo1.Lines do
  begin
    Add(jo.Format(‘%titleNoFormatting%:‘));
    Add(jo.Format(‘%unescapedUrl%‘));
    Add(EmptyStr);
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  u = ‘http://www.google.cn/uds/GwebSearch?callback=response&rsz=large&v=1.0&q=‘;
var
  jo: ISuperObject;
  req: IXMLHTTPRequest;
  url: WideString;
begin
  jo := SO;
  jo.M[‘response‘] := @Proc; {搜索结果将是类似 response(...) 函数格式的字符串}

  url := u + ToUTF8Encode(Edit1.Text); {准备搜索地址}

  //搜索
  req := CoXMLHTTP.Create;
  req.open(‘Get‘, url, False, EmptyParam, EmptyParam);
  req.send(EmptyParam);

  //搜索结果在 req.responseText(后付其全部内容), 下面语句将调用上面的 Proc 过程.
  jo[req.responseText]; {这是在本系列"方法"一节用到的第二种调用方法}
end;

end.


窗体文件:


object Form1: TForm1
  Left = 0
  Top = 0
  Caption = ‘Form1‘
  ClientHeight = 358
  ClientWidth = 547
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = ‘Tahoma‘
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Memo1: TMemo
    Left = 0
    Top = 0
    Width = 412
    Height = 358
    Align = alLeft
    Lines.Strings = (
      ‘Memo1‘)
    ScrollBars = ssBoth
    TabOrder = 0
    ExplicitHeight = 346
  end
  object Button1: TButton
    Left = 440
    Top = 72
    Width = 75
    Height = 25
    Caption = ‘Button1‘
    TabOrder = 1
    OnClick = Button1Click
  end
  object Edit1: TEdit
    Left = 418
    Top = 24
    Width = 121
    Height = 21
    TabOrder = 2
    Text = ‘Delphi‘
  end
end


下面是我以 "Delphi" 为关键字搜索返回的 responseText, 可以做个搜索工具了:

response(
{
"responseStatus": 200,
"responseData": {
"cursor": {
"pages": [
{
"label": 1,
"start": "0"
},{
"label": 2,
"start": "8"
},{
"label": 3,
"start": "16"
},{
"label": 4,
"start": "24"
},{
"label": 5,
"start": "32"
},{
"label": 6,
"start": "40"
},{
"label": 7,
"start": "48"
},{
"label": 8,
"start": "56"
}],
"currentPageIndex": 0,
"estimatedResultCount": "6050000",
"moreResultsUrl": "http:\/\/www.google.cn\/search?oe=utf8&ie=utf8&source=uds&start=0&hl=zh-CN&q=Delphi"
},
"results": [
{
"titleNoFormatting": "『Delphi园地』-源码,控件,文档,工具,免费下载,建设专业Delphi资源平台",
"url": "http:\/\/www.delphifans.com\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:gxlSlu0OTqQJ:www.delphifans.com",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "www.delphifans.com",
"content": "提供Delhpi技巧文章、源码、控件、文档、工具等。",
"title": "『<b>Delphi<\/b>园地』-源码,控件,文档,工具,免费下载,建设专业<b>Delphi<\/b>资源平台",
"unescapedUrl": "http:\/\/www.delphifans.com\/"
},{
"titleNoFormatting": "Delphi - 维基百科,自由的百科全书",
"url": "http:\/\/zh.wikipedia.org\/wiki\/Delphi",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:KPWnJZ_ZuuQJ:zh.wikipedia.org",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "zh.wikipedia.org",
"content": "<b>Delphi<\/b>,是Windows平台下的著名快速应用程序开发工具(Rapid Application Development ,简称RAD)。它的前身是在DOS下的产品BorlandTurbo Pascal,最早的版本由 <b>...<\/b>",
"title": "<b>Delphi<\/b> - 维基百科,自由的百科全书",
"unescapedUrl": "http:\/\/zh.wikipedia.org\/wiki\/Delphi"
},{
"titleNoFormatting": "DELPHI盒子- Delphi源代码控件文档工具下载",
"url": "http:\/\/www.2ccc.com\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:CF650RgvoJkJ:www.2ccc.com",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "www.2ccc.com",
"content": "<b>DELPHI<\/b>源代码、组件、例子、演示及相关工具下载,文档发布,全站包含论坛,笔记等功能 ,每天更新。",
"title": "<b>DELPHI<\/b>盒子- <b>Delphi<\/b>源代码控件文档工具下载",
"unescapedUrl": "http:\/\/www.2ccc.com\/"
},{
"titleNoFormatting": "编程中国>> 技术教程>> 开发语言>> Delphi",
"url": "http:\/\/www.bccn.net\/Article\/kfyy\/delphi\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:vjiBcr2xV14J:www.bccn.net",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "www.bccn.net",
"content": "普通文章, 第二章<b>Delphi<\/b>面向对象的编程方法(一), 06-25. 普通文章 <b>...<\/b> 普通文章, 用 <b>Delphi<\/b>制作以浏览器为界面的应用程序, 08-30. 普通文章, <b>Delphi<\/b> 中串口通讯的实现 <b>...<\/b>",
"title": "编程中国>> 技术教程>> 开发语言>> <b>Delphi<\/b>",
"unescapedUrl": "http:\/\/www.bccn.net\/Article\/kfyy\/delphi\/"
},{
"titleNoFormatting": "Delphi is a leading global supplier for the automotive, computing ...",
"url": "http:\/\/www.delphi.com\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:r5kD35m3Ns0J:www.delphi.com",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "www.delphi.com",
"content": "Supplier of audio systems, modules, and components to manufacturers and replacement parts to the after market worldwide.",
"title": "<b>Delphi<\/b> is a leading global supplier for the automotive, computing <b>...<\/b>",
"unescapedUrl": "http:\/\/www.delphi.com\/"
},{
"titleNoFormatting": "delphi 窑洞",
"url": "http:\/\/www.51delphi.com\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:Kgv5hBXmSFgJ:www.51delphi.com",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "www.51delphi.com",
"content": "基于<b>Delphi<\/b> 的Rich Internet Application开发和应用平台 。 <b>...<\/b> 61449, <b>Delphi<\/b> 7的 Property OldCreateOrder does not exist(1786字节), javer007, 2009-10-12 14:42:48 <b>...<\/b>",
"title": "<b>delphi<\/b> 窑洞",
"unescapedUrl": "http:\/\/www.51delphi.com\/"
},{
"titleNoFormatting": "delphi专栏_软件开发_最专业的程序开发网站_开发者网络_软件_天极网",
"url": "http:\/\/dev.yesky.com\/ysdevdelphi\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:j-qd-w9ESrwJ:dev.yesky.com",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "dev.yesky.com",
"content": "在<b>Delphi<\/b>数据库应用系统的数据录入过程中,有些数据是保存在Excel中的;有些是保存 <b>...<\/b> 为了便于开发数据库应用程序,在<b>Delphi<\/b>集成开发环境中,提供了大量的数据库工具 <b>...<\/b>",
"title": "<b>delphi<\/b>专栏_软件开发_最专业的程序开发网站_开发者网络_软件_天极网",
"unescapedUrl": "http:\/\/dev.yesky.com\/ysdevdelphi\/"
},{
"titleNoFormatting": "Delphi俱乐部",
"url": "http:\/\/www.tommstudio.com\/",
"cacheUrl": "http:\/\/www.google.com\/search?q=cache:9JQ4L48RA1QJ:www.tommstudio.com",
"GsearchResultClass": "GwebSearch",
"visibleUrl": "www.tommstudio.com",
"content": "dephi俱乐部,c#,<b>delphi<\/b>,<b>delphi<\/b>控件,<b>delphi<\/b>源码,<b>delphi<\/b>书籍,商业源码,程序开发,源码 交易,开发,方案, <b>...<\/b> Net,Java,PowerBuilder,PHP,ASP,PERL 免费源码下载,<b>Delphi<\/b>俱乐部 .",
"title": "<b>Delphi<\/b>俱乐部",
"unescapedUrl": "http:\/\/www.tommstudio.com\/"
}]
},
"responseDetails": null
}
)

XE3随笔17:实例 - 模拟 Google 搜索

标签:

原文地址:http://www.cnblogs.com/DaXiong2000/p/4726508.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!