Discuz教程网

php抓取天气预报(天气,气温,风力,日出日落...)

[复制链接]
authicon dly 发表于 2013-2-21 22:38:03 | 显示全部楼层 |阅读模式
  1. /**
  2. *php抓取天气预报(天气,气温,风力,日出日落...)
  3. *
  4. *第一天没有最高气温数据,第八天没有最低气温数据
  5. *注意对数字进行过滤时不要忘记对负号进行判断
  6. *对风力过滤时要考虑到3-5级这种格式
  7. */
  8. class weatherfetch
  9. {
  10.         private $f;
  11.         function getNum ( $string )
  12.         {
  13.                 $tmpstr = '';
  14.                 $strlen = strlen ( $string );
  15.                 for ( $i=0; $i<$strlen; $i++ )
  16.                 {
  17.                         $str=substr ( $string, $i, 1 );
  18.                         $str1=trim ( $str );
  19.                         if ( is_numeric ( $str1 ) )
  20.                         {
  21.                                 $tmpstr.=$str1+0;

  22.                         }
  23.                         if ( $str1=="-"&&is_numeric ( substr ( $string, $i-1, 1 ) ) )
  24.                         {
  25.                                 $tmpstr.= $str1;
  26.                         }

  27.                 }
  28.                 return $tmpstr;
  29.         }
  30.         function __construct()
  31.         {
  32.                 $this->f= new SaeFetchurl();

  33.         }
  34.         function getChineseNum ( $string )
  35.         {
  36.                 $tmpstr = '';
  37.                 $arr = array ( 1,2,3,4,5,6,7,8,9,0 );
  38.                 $strlen = strlen ( $string );
  39.                 for ( $i=0; $i<$strlen; $i++ )
  40.                 {

  41.                         $str=substr ( $string, $i, 1 );

  42.                         $str1=trim ( $str );
  43.                         if ( ord ( $str ) >0xA0 )
  44.                         {

  45.                                 $tmpstr.= substr ( $string, $i, 3 );

  46.                                 $i = $i+2;

  47.                         }

  48.                         if ( is_numeric ( $str1 ) )
  49.                         {

  50.                                 $tmpstr.= $str1;

  51.                         }
  52.                         if ( $str1=="-"&&is_numeric ( substr ( $string, $i-1, 1 ) ) &&is_numeric ( substr ( $string, $i+1, 1 ) ) )
  53.                         {
  54.                                 $tmpstr.= $str1;
  55.                         }

  56.                 }

  57.                 return $tmpstr;

  58.         }
  59.         function getChinese ( $string,$encode="GBK" )
  60.         {
  61.                 switch ( $encode )
  62.                 {
  63.                 case "GBK" :
  64.                         $codelength=2;
  65.                         break;
  66.                 case "GB2312" :
  67.                         $codelength=3;
  68.                         break;
  69.                 case "UTF-8" :
  70.                         $codelength=3;
  71.                         break;
  72.                 case "UTF-16" :
  73.                         $codelength=4;
  74.                         break;

  75.                 }
  76.                 $tmpstr = '';
  77.                 $arr = array ( 1,2,3,4,5,6,7,8,9,0 );
  78.                 $strlen = strlen ( $string );
  79.                 for ( $i=0; $i<$strlen; $i++ )
  80.                 {
  81.                         $str=substr ( $string, $i, 1 );
  82.                         $str1=trim ( $str );
  83.                         if ( ord ( $str ) >0xA0 )
  84.                         {
  85.                                 $tmpstr.= substr ( $string, $i, $codelength );
  86.                                 $i = $i+$codelength-1;
  87.                         }

  88.                 }
  89.                 return $tmpstr;
  90.         }
  91.         function get ( $cityid )
  92.         {
  93.                 $url="http://www.weather.com.cn/weather/".$cityid.".shtml";
  94.                 $data=$this->f->fetch ( $url );

  95.                 $sun=explode ( '
  96.                                ',$data );
  97.                 $sun=explode ( "
  98.                                ",$sun[1] );
  99.                 $sun=explode ( "
  100.                                ",$sun[1] );
  101.                 $sun=explode ( "",$sun[0] );
  102.                 $sunrise=strlen ( $sun[0] );
  103.                 $sunrise=substr ( $sun[0],$sunrise-5 );//日出时间
  104.                 $sunset=strlen ( $sun[1] );
  105.                 $sunset=substr ( $sun[1],$sunset-5 );//日落时间
  106.                 $sunhour=substr ( $sunset,0,2 )-substr ( $sunrise,0,2 );
  107.                 $sunminute=$sunhour*60+substr ( $sunset,-2 )-substr ( $sunrise,-2 );//日照时间
  108.                 $yubao=explode ( 'class="yuBaoTable"',$data );
  109.                 $num=count ( $yubao );
  110.                 $tl=array();
  111.                 $th=array();
  112.                 $fx=array();
  113.                 $fl=array();
  114.                 $weather=array();
  115.                 //第一天
  116.                 $tr=explode ( "",$yubao[1] );
  117.                 $td=explode ( "",$tr[0] );
  118.                 $weather[]=$this->getChinese ( $td[3],"UTF-8" );//晚上天气
  119.                 $fx[]=$this->getChinese ( $td[5],"UTF-8" );//晚上风向
  120.                 $fl[]=substr ( $this->getChineseNum ( $td[6],"UTF-8" ),5 );//晚上风力
  121.                 $tltemp=explode ( "",$td[4] );//最低气温
  122.                 $tl[]=$this->getNum ( $tltemp[1] );
  123.                 //从第二天到第七天
  124.                 for ( $i=2; $i<$num-1; $i++ )
  125.                 {
  126.                         $tr=explode ( "",$yubao[$i] );
  127.                         $td=explode ( "",$tr[0] );
  128.                         $weather[]=$this->getChinese ( $td[3],"UTF-8" );//白天天气
  129.                         $fx[]=$this->getChinese ( $td[5],"UTF-8" );//白天风向
  130.                         $fltemp=substr ( $this->getChineseNum ( $td[6],"UTF-8" ),5 );
  131.                         $fl[]=$fltemp;//白天风力
  132.                         $thtemp=explode ( "",$td[4] );
  133.                         $th[]=$this->getNum ( $thtemp[1] );//最高气温
  134.                         $td=explode ( "",$tr[1] );
  135.                         $tltemp=explode ( "",$td[3] );
  136.                         $tl[]=$this->getNum ( $tltemp[1] );//最低气温

  137.                 }
  138.                 //第八天
  139.                 $tr=explode ( "",$yubao[$num-1] );
  140.                 $td=explode ( "",$tr[0] );
  141.                 $weather[]=$this->getChinese ( $td[3],"UTF-8" );//白天天气
  142.                 $fx[]=$this->getChinese ( $td[5],"UTF-8" );//白天风向
  143.                 $fl[]=substr ( $this->getChineseNum ( $td[6],"UTF-8" ),5 );//白天风力
  144.                 $thtemp=explode ( "",$td[4] );
  145.                 $th[]=$this->getNum ( $thtemp[1] );//最高气温
  146.                 if ( count ( $weather ) ==8 )
  147.                 {
  148.                         return array ( "weather"=>$weather,"tl"=>$tl,"th"=>$th,"fx"=>$fx,"fl"=>$fl,"sunset"=>$sunset,"sunrise"=>$sunrise,"sunminute"=>$sunminute );
  149.                 }
  150.                 else
  151.                 {
  152.                         return 1;

  153.                 }
  154.         }
  155.         function getday1 ( $cityid )
  156.         {
  157.                 $url="http://www.weather.com.cn/weather/".$cityid.".shtml";
  158.                 $data=$this->f->fetch ( $url );
  159.                 $yubao=explode ( 'class="yuBaoTable"',$data );
  160.                 $tr=explode ( "",$yubao[1] );
  161.                 $td=explode ( "",$tr[0] );
  162.                 $thtemp=explode ( "",$td[4] );
  163.                 return $this->getNum ( $thtemp[1] );

  164.         }
  165. }
复制代码





上一篇:php获取各城市72小时天气预报[中国天气网]
下一篇:利用中央气象台的天气预报API实现天气预报
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-2 11:15

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表