JSON 之 SuperObject(16): 實例 - 解析 Google 關鍵字搜索排名

同上例相似, 經過 'http://clients1.google.cn/complete/search?&q=' + "關鍵字" 能夠獲取 Google 的關鍵字搜索排名.

我用 Delphi 爲關鍵字獲得的結果是:ide


window.google.ac.h(
  ["Delphi",[
      ["delphi 教程", "375,000 結果", "0"],
      ["delphi盒子", "74,900 結果", "1"],
      ["delphi 下載", "1,580,000 結果", "2"],
      ["delphi7 下載", "1,600,000 結果", "3"],
      ["delphi是什麼", "497,000 結果", "4"],
      ["delphi 字符串函數", "352,000 結果", "5"],
      ["delphi7 序列號", "302,000 結果", "6"],
      ["delphi2009下載", "20,600 結果", "7"],
      ["delphi7", "1,330,000 結果","8"],
      ["delphi2009正式版下載", "5,710 結果", "9"]
    ]
  ]
)


上面結果以 window.google.ac.h(...) 的形式給出, 下面的操做關鍵就是給 ISuperObject 一個名爲 "window.google.ac.h" 的方法, 並指向自定義的過程, 並在過程當中完成解析.函數



運行效果圖:

google


代碼文件:url


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;

function ToUTF8Encode(str: string): string;
var
  b: Byte;
begin
  for b in BytesOf(UTF8Encode(str)) do
    Result := Format('%s%s%.2x', [Result, '%', b]);
end;

procedure Proc(const This, Params: ISuperObject; var Result: ISuperObject);
var
  jo: ISuperObject;
begin
  Form1.Memo1.Clear;
  for jo in Params['1'] do with Form1.Memo1.Lines do
    Form1.Memo1.Lines.Add(jo.format('%2%: %0% - %1%'));
end;

procedure TForm1.Button1Click(Sender: TObject);
const
  u = 'http://clients1.google.cn/complete/search?&q=';
var
  jo: ISuperObject;
  req: IXMLHTTPRequest;
  url: WideString;
begin
  jo := SO;
  jo.M['window.google.ac.h'] := @Proc;

  url := u + ToUTF8Encode(Edit1.Text);

  req := CoXMLHTTP.Create;
  req.open('Get', url, False, EmptyParam, EmptyParam);
  req.send(EmptyParam);

  jo[req.responseText];
end;

end.
相關文章
相關標籤/搜索