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 之 热键组件(thotkey)

Delphi 如何从html格式的字符串中提取纯文本?

Delphi 控制台读写

Delphi 安装apk

Delphi中对进程内存进行读写

Delphi xe 无法编译apk提示sdk路径问题时?sdk路径配置方法

Delphi中实现调整图像的色阶的算法

Delphi通过ip或机器名获取局域网mac地址

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

Delphi 读取剪粘板内的html格式数据

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



打赏

取消

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

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

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

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

评论

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