Delphi通过解析搜狐网页来获取天气的函数

{
  获取城市的天气
  //使用方法
  Edit1.text:=GetWeather('北京');

}

 

 1 function GetWeather(City: String): String;
 2 const
 3    sys01='<table border=0 cellpadding=0 cellspacing=0 width=186 bgcolor=#ffffff>';
 4    sys02='<img src="';
 5    sys03='" width=';
 6    sys04='<td class=blu01>';
 7    sys05='</td>';
 8 var
 9  tmpstr,str1:string;
10  j:integer;
11  vFileStream:TFileStream;
12  http:tidhttp;
13 begin
14  http:=tidhttp.Create(nil); 
15  tmpstr:=http.Get('http://weather.news.sohu.com/citymake.php?city='+City);
16  j:=pos(sys01,tmpstr);
17  tmpstr:=copy(tmpstr,j,length(tmpstr));
18  j:=pos(sys02, tmpstr);
19  tmpstr:=copy(tmpstr,j+length(sys02),length(tmpstr));
20  j:=pos(sys03,tmpstr);
21  str1:=copy(tmpstr,0,j-1);
22 
23  //下载图片
24   vFileStream:=TFileStream.Create(ExtractFilePath(application.ExeName)+'/weather.jpg',fmCreate,fmShareDenyNone);
25   try
26     //IdHTTP1.Get('http://173.16.80.26/inputdb.mdb',vFileStream);
27     HTTP.Get(str1,vFileStream);
28    //mage1.Picture.Bitmap.LoadFromStream(vfilestream);
29   finally
30    vFileStream.Free;
31   end;//try..finally
32  {sleep(1000);
33  image1.Picture.LoadFromFile( ExtractFilePath(application.ExeName)+'/weather.jpg');
34  }
35  j:=pos(sys04,tmpstr);
36  tmpstr:=copy(tmpstr, j+length(sys04),length(tmpstr));
37  j:=pos(sys05,tmpstr);
38  str1:=copy(tmpstr,0,j-1);
39  str1:=stringreplace(str1,' ','',[rfreplaceall]);
40  str1:=stringreplace(str1,chr(13),'',[rfreplaceall]);
41  str1:=stringreplace(str1,chr(10),'',[rfreplaceall]);
42  str1:=stringreplace(str1,'<br>','  ',[rfreplaceall]);
43  http.Free;
44  result:=str1;
45 end;

posted on 2008-09-18 18:43  流浪猫  阅读(992)  评论(0编辑  收藏  举报

导航