其实那些内置天气预报功能的软件的天气预报功能也都是来源于网上!因为也没有哪个软件公司会自己去架一个气象站了哈哈,现在我就来说说如何通过互联网上的信息来获取天气情况!
目前能查询天气的网站有不少,比较有名的有中国天气网与Google天气,大部分站点与大部分软件的天气预报数据也都是来源于中国天气网!因为中国天气网是中国气象局相关的,所以他的数据会更准确一些!查阅本文信息时,假设读者熟悉http协议与如何模拟网页提交等知识!
中国天气网有一个连接http://m.weather.com.cn/data/+‘城市编码.html',这个连接可以获取对应的城市的天气情况!返回Json数据,解析Json就能获得该城市的天气!这个问题就在那个城市编码的获取方式上,这个获取方式,网上有代码的!我就不写了,我是直接将城市编码都包到资源文件中去了使用的时候,直接从资源加载就行了!获得天气的Json数据,用的事Http协议,这里只要用可以提交http的数据的控件或者类都可以(比如,indy的idhttp,ics的http控件都行,也可以使用MSxml的IXMLHttpRequest,也可以使用HttpRequest5.1或者使用WinHttp控件),这里我用的事最简单的方式,就用了Delphi直接带的indy的idhttp来获得
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
constructorCreate(AOwner: TComponent);override;
destructorDestroy;override;
procedureLoadFromFile(Filename: string);
procedureLoadFromRes(ResName: string);
end;
TForm4 = class(TForm)
Memo1:TMemo;
Panel1:TPanel;
ComboBox1:TComboBox;
Button1:TButton;
IdHTTP1:TIdHTTP;
procedureFormCreate(Sender: TObject);
procedureButton1Click(Sender: TObject);
private
{ Privatedeclarations }
functionpro_result(str:string):string ;
public
{ Publicdeclarations }
List:TStringList;
HttpReq:IXMLHttpRequest;
end;
var
Form4: TForm4;
implementation
uses DateUtils;
{$R *.dfm}
{$R mm.RES}
procedure TForm4.Button1Click(Sender: TObject);
var
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) asTlkJSONobject;
ChildJson := Json.Field['weatherinfo'];
Memo1.Lines.Clear;
if ChildJson.SelfType = jsObject then
begin
//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 Truethen
//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));
end;
end;
procedure TForm4.FormCreate(Sender: TObject);
var
temp,str_1,str_2:string;
http1:tidhttp;
i:Integer;
begin
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 <> 0then
temp :=copy(Caption,i+1,MaxInt)
else temp := Caption;
i := Pos('市',temp);
if i <> 0then
temp :=copy(temp,0,i-1);
ComboBox1.ItemIndex :=ComboBox1.Items.IndexOf(temp);
HttpReq := CoXMLHTTPRequest.Create;
Button1.Click;
end;
function TForm4.pro_result(str: string): string;
var
i_0,i_1:Integer;
temp_result:string;
begin
temp_result:='';
whilePos('查询结果',str)<>0 do
begin
i_1:=Pos('</span>',str);
i_0:=Pos('查询结果',str);
temp_result:=temp_result+Copy(str,i_0,i_1-i_0)+#13#10;
Delete(str,1,Pos('</span>',str));
end;
result:=StringReplace(temp_result,' ','',[rfReplaceAll]);
i_1 := Pos('-',Result);
Result := copy(Result,i_1+1,MaxInt);
end;
{ TComboBox }
constructor TComboBox.Create(AOwner: TComponent);
begin
inherited;
Values := TStringList.Create;
end;
destructor TComboBox.Destroy;
begin
Values.Free;
inherited;
end;
procedure TComboBox.LoadFromFile(Filename: string);
var
i: Integer;
begin
Items.Clear;
Values.LoadFromFile(Filename);
for i := 0 to Values.Count - 1 do
begin
Items.Add(Values.Names[i]);
end;
ItemIndex := 0;
end;
procedure TComboBox.LoadFromRes(ResName: string);
var
stream: TResourceStream;
i: Integer;
begin
stream :=TResourceStream.Create(HInstance,'CityCode','TxtData');
Items.Clear;
Values.LoadFromStream(stream);
for i := 0 to Values.Count - 1 do
begin
Items.Add(Values.Names[i]);
end;
ItemIndex := 0;
stream.Free;
end;