本文整理自网络,侵删。
{代码可能失效,重在学习方法 获取城市的天气 //使用方法 Edit1.text:=GetWeather('北京');}
function GetWeather(City: String): String;const sys01='<table border=0 cellpadding=0 cellspacing=0 width=186 bgcolor=#ffffff>'; sys02='<img src="'; sys03='" width='; sys04='<td class=blu01>'; sys05='</td>';var tmpstr,str1:string; j:integer; vFileStream:TFileStream; http:tidhttp;begin http:=tidhttp.Create(nil); tmpstr:=http.Get('http://weather.news.sohu.com/citymake.php?city='+City); j:=pos(sys01,tmpstr); tmpstr:=copy(tmpstr,j,length(tmpstr)); j:=pos(sys02, tmpstr); tmpstr:=copy(tmpstr,j+length(sys02),length(tmpstr)); j:=pos(sys03,tmpstr); str1:=copy(tmpstr,0,j-1);
//下载图片 vFileStream:=TFileStream.Create(ExtractFilePath(application.ExeName)+'/weather.jpg',fmCreate,fmShareDenyNone); try //IdHTTP1.Get('http://173.16.80.26/inputdb.mdb',vFileStream); HTTP.Get(str1,vFileStream); //mage1.Picture.Bitmap.LoadFromStream(vfilestream); finally vFileStream.Free; end;//try..finally {sleep(1000); image1.Picture.LoadFromFile( ExtractFilePath(application.ExeName)+'/weather.jpg'); } j:=pos(sys04,tmpstr); tmpstr:=copy(tmpstr, j+length(sys04),length(tmpstr)); j:=pos(sys05,tmpstr); str1:=copy(tmpstr,0,j-1); str1:=stringreplace(str1,' ','',[rfreplaceall]); str1:=stringreplace(str1,chr(13),'',[rfreplaceall]); str1:=stringreplace(str1,chr(10),'',[rfreplaceall]); str1:=stringreplace(str1,'<br>',' ',[rfreplaceall]); http.Free; result:=str1;end;
相关阅读 >>
Delphi xe 无法编译apk提示sdk路径问题时?sdk路径配置方法
Delphi 解决timage无法在窗体resize时改变canvas的大小
更多相关阅读请进入《Delphi》频道 >>