天天看點

Delphi天氣預報查詢

   現在的很多軟體中都内置了天氣檢視這個功能!其實呢,這個功能的實作并不麻煩!會上網的人,都會查天氣情況!其實那些内置天氣預報功能的軟體的天氣預報功能也都是來源于網上!因為也沒有哪個軟體公司會自己去架一個氣象站了哈哈,現在我就來說說如何通過網際網路上的資訊來擷取天氣情況!

  目前能查詢天氣的網站有不少,比較有名的有中國天氣網與Google天氣,大部分站點與大部分軟體的天氣預報資料也都是來源于中國天氣網!因為中國天氣網是中國氣象局相關的,是以他的資料會更準确一些!查閱本文資訊時,假設讀者熟悉http協定與如何模拟網頁送出等知識!

<a></a>

unit Unit4;

interface

uses

Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

Dialogs, StdCtrls,msxml,uLkJSON, ExtCtrls, IdBaseComponent, IdComponent,

IdTCPConnection, IdTCPClient, IdHTTP;

type

TComboBox = class(StdCtrls.TComboBox)

private

Values: TStringList;

public

constructor Create(AOwner: TComponent);override;

destructor Destroy;override;

procedure LoadFromFile(Filename: string);

procedure LoadFromRes(ResName: string);

end;

TForm4 = class(TForm)

Memo1: TMemo;

Panel1: TPanel;

ComboBox1: TComboBox;

Button1: TButton;

IdHTTP1: TIdHTTP;

procedure FormCreate(Sender: TObject);

procedure Button1Click(Sender: TObject);

{ Private declarations }

function pro_result(str:string):string ;

{ Public declarations }

List: TStringList;

HttpReq: IXMLHttpRequest;

var

Form4: TForm4;

implementation

uses DateUtils;

{$R *.dfm}

{$R mm.RES}

procedure TForm4.Button1Click(Sender: TObject);

url: string;

Json: TlkJSONobject;

ChildJson,tmpJson: TlkJSONbase;

begin

url := 'http://m.weather.com.cn/data/'+Combobox1.Values.ValueFromIndex[ComboBox1.ItemIndex]+'.html';

HttpReq.open('Get', Url, False, EmptyParam, EmptyParam);

HttpReq.send(EmptyParam);//開始搜尋

Url := HttpReq.responseText;

Json := Tlkjson.ParseText(url) as TlkJSONobject;

ChildJson := Json.Field['weatherinfo'];

Memo1.Lines.Clear;

if ChildJson.SelfType = jsObject then

//Showmessage(ChildJson.Field['city'].Value);

Memo1.Lines.Add('今日天氣('+Vartostr(ChildJson.Field['date_y'].Value)+' '+Vartostr(ChildJson.Field['week'].Value)+'):');

Memo1.Lines.Add(' 溫度:'+Vartostr(ChildJson.Field['temp1'].Value));

Memo1.Lines.Add(' 天氣:'+Vartostr(ChildJson.Field['weather1'].Value));

//Memo1.Lines.Add(' 風向:'+Vartostr(ChildJson.Field['fx1'].Value)+ ' '+Vartostr(ChildJson.Field['wind1'].Value));

Memo1.Lines.Add(' 風力:'+Vartostr(ChildJson.Field['wind1'].Value));

Memo1.Lines.Add('明日天氣('+FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now))+'):');

Memo1.Lines.Add(' 溫度:'+Vartostr(ChildJson.Field['temp2'].Value));

Memo1.Lines.Add(' 天氣:'+Vartostr(ChildJson.Field['weather2'].Value));

//Memo1.Lines.Add(' 風向:'+Vartostr(ChildJson.Field['fx2'].Value)+ ' '+Vartostr(ChildJson.Field['wind2'].Value));

Memo1.Lines.Add(' 風力:'+Vartostr(ChildJson.Field['wind2'].Value));

Memo1.Lines.Add(FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now,2))+':');

Memo1.Lines.Add(' 溫度:'+Vartostr(ChildJson.Field['temp3'].Value));

Memo1.Lines.Add(' 天氣:'+Vartostr(ChildJson.Field['weather3'].Value));

//if True then

//Memo1.Lines.Add(' 風向:'+Vartostr(ChildJson.Field['fx3'].Value)+ ' '+Vartostr(ChildJson.Field['wind3'].Value));

Memo1.Lines.Add(' 風力:'+Vartostr(ChildJson.Field['wind3'].Value));

Memo1.Lines.Add(FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now,3))+':');

Memo1.Lines.Add(' 溫度:'+Vartostr(ChildJson.Field['temp4'].Value));

Memo1.Lines.Add(' 天氣:'+Vartostr(ChildJson.Field['weather4'].Value));

//Memo1.Lines.Add(' 風向:'+Vartostr(ChildJson.Field['fx4'].Value)+ ' '+Vartostr(ChildJson.Field['wind4'].Value));

Memo1.Lines.Add(' 風力:'+Vartostr(ChildJson.Field['wind4'].Value));

Memo1.Lines.Add(FormatDateTime('YYYY年MM月DD日 ',DateUtils.IncDay(now,4))+':');

Memo1.Lines.Add(' 溫度:'+Vartostr(ChildJson.Field['temp5'].Value));

Memo1.Lines.Add(' 天氣:'+Vartostr(ChildJson.Field['weather5'].Value));

//Memo1.Lines.Add(' 風向:'+Vartostr(ChildJson.Field['fx5'].Value)+ ' '+Vartostr(ChildJson.Field['wind5'].Value));

Memo1.Lines.Add(' 風力:'+Vartostr(ChildJson.Field['wind5'].Value));

procedure TForm4.FormCreate(Sender: TObject);

temp,str_1,str_2:string;

http1:tidhttp;

i:Integer;

http1:=tidhttp.create(self);

temp:=HTTP1.Get('http://www.ipseeker.cn');

http1.free;

//temp:=Mmo1.Text;

i:=Pos('查詢結果',temp);

str_1:=Copy(temp,i,254);

str_2:=pro_result(str_1);

//Mmo2.Text:=str_2;

Caption := Trim(str_2);

ComboBox1.LoadFromRes('CityCode.Data');

i := Pos(' ',Caption);

if i &lt;&gt; 0 then

temp := copy(Caption,i+1,MaxInt)

else temp := Caption;

i := Pos('市',temp);

temp := copy(temp,0,i-1);

ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(temp);

HttpReq := CoXMLHTTPRequest.Create;

Button1.Click;

function TForm4.pro_result(str: string): string;

i_0,i_1:Integer;

temp_result:string;

temp_result:='';

while Pos('查詢結果',str)&lt;&gt;0 do

i_1:=Pos('&lt;/span&gt;',str);

i_0:=Pos('查詢結果',str);

temp_result:=temp_result+Copy(str,i_0,i_1-i_0)+#13#10;

Delete(str,1,Pos('&lt;/span&gt;',str));

result:=StringReplace(temp_result,'&amp;nbsp;','',[rfReplaceAll]);

i_1 := Pos('-',Result);

Result := copy(Result,i_1+1,MaxInt);

{ TComboBox }

constructor TComboBox.Create(AOwner: TComponent);

inherited;

Values := TStringList.Create;

destructor TComboBox.Destroy;

Values.Free;

procedure TComboBox.LoadFromFile(Filename: string);

i: Integer;

Items.Clear;

Values.LoadFromFile(Filename);

for i := 0 to Values.Count - 1 do

Items.Add(Values.Names[i]);

ItemIndex := 0;

procedure TComboBox.LoadFromRes(ResName: string);

stream: TResourceStream;

stream := TResourceStream.Create(HInstance,'CityCode','TxtData');

Values.LoadFromStream(stream);

stream.Free;

end.