现在的很多软件中都内置了天气查看这个功能!其实呢,这个功能的实现并不麻烦!会上网的人,都会查天气情况!其实那些内置天气预报功能的软件的天气预报功能也都是来源于网上!因为也没有哪个软件公司会自己去架一个气象站了哈哈,现在我就来说说如何通过互联网上的信息来获取天气情况!
目前能查询天气的网站有不少,比较有名的有中国天气网与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 <> 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)<>0 do
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));
result:=StringReplace(temp_result,'&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.