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


本文整理自网络,侵删。

 

{代码可能失效,重在学习方法
  获取城市的天气
  //使用方法
  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 xe7的android应用设定wifi的有效无效

Delphi获取我的文档路径

Delphi怎么实现一定范围内的随机数呢?

Delphi 解决timage无法在窗体resize时改变canvas的大小

Delphi从excel读取数据存入数据库demo

Delphi firedac 获取 insert 记录的自增 id

Delphi 用input连续记录所输入的次数

Delphi 中主设置静音的方式

Delphi 为richedit设置行号

Delphi memo1自动循环上下滚屏

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...