完整代码:php抓取天气预报(天气,气温,风力,日出日落...) - 云代码

 

获取一周内的各种天气预报参数,非api模式。

 

 

 
  
  1. class weatherfetch  
  2. {  
  3.     private $f;  
  4.     function getNum ( $string )  
  5.     {  
  6.         $tmpstr = '';  
  7.         $strlen = strlen ( $string );  
  8.         for ( $i=0; $i<$strlen$i++ )  
  9.         {  
  10.             $str=substr ( $string$i, 1 );  
  11.             $str1=trim ( $str );  
  12.             if ( is_numeric ( $str1 ) )  
  13.             {  
  14.                 $tmpstr.=$str1+0;  
  15.  
  16.             }  
  17.             if ( $str1=="-"&&is_numeric ( substr ( $string$i-1, 1 ) ) )  
  18.             {  
  19.                 $tmpstr.= $str1;  
  20.             }  
  21.  
  22.         }  
  23.         return $tmpstr;  
  24.     }  
  25.     function __construct()  
  26.     {  
  27.         $this->f= new SaeFetchurl();  
  28.  
  29.     }  
  30.     function getChineseNum ( $string )  
  31.     {  
  32.         $tmpstr = '';  
  33.         $arr = array ( 1,2,3,4,5,6,7,8,9,0 );  
  34.         $strlen = strlen ( $string );  
  35.         for ( $i=0; $i<$strlen$i++ )  
  36.         {  
  37.  
  38.             $str=substr ( $string$i, 1 );  
  39.  
  40.             $str1=trim ( $str );  
  41.             if ( ord ( $str ) >0xA0 )  
  42.             {  
  43.  
  44.                 $tmpstr.= substr ( $string$i, 3 );  
  45.  
  46.                 $i = $i+2;  
  47.  
  48.             }  
  49.  
  50.             if ( is_numeric ( $str1 ) )  
  51.             {  
  52.  
  53.                 $tmpstr.= $str1;  
  54.  
  55.             }  
  56.             if ( $str1=="-"&&is_numeric ( substr ( $string$i-1, 1 ) ) &&is_numeric ( substr ( $string$i+1, 1 ) ) )  
  57.             {  
  58.                 $tmpstr.= $str1;  
  59.             }  
  60.  
  61.         }  
  62.  
  63.         return $tmpstr;  
  64.  
  65.     }  
  66.     function getChinese ( $string,$encode="GBK" )  
  67.     {  
  68.         switch ( $encode )  
  69.         {  
  70.         case "GBK" :  
  71.             $codelength=2;  
  72.             break;  
  73.         case "GB2312" :  
  74.             $codelength=3;  
  75.             break;  
  76.         case "UTF-8" :  
  77.             $codelength=3;  
  78.             break;  
  79.         case "UTF-16" :  
  80.             $codelength=4;  
  81.             break;  
  82.  
  83.         }  
  84.         $tmpstr = '';  
  85.         $arr = array ( 1,2,3,4,5,6,7,8,9,0 );  
  86.         $strlen = strlen ( $string );  
  87.         for ( $i=0; $i<$strlen$i++ )  
  88.         {  
  89.             $str=substr ( $string$i, 1 );  
  90.             $str1=trim ( $str );  
  91.             if ( ord ( $str ) >0xA0 )  
  92.             {  
  93.                 $tmpstr.= substr ( $string$i$codelength );  
  94.                 $i = $i+$codelength-1;  
  95.             }  
  96.  
  97.         }  
  98.         return $tmpstr;  
  99.     }  
  100.     function get ( $cityid )  
  101.     {  
  102.         $url="http://www.weather.com.cn/weather/".$cityid.".shtml";  
  103.         $data=$this->f->fetch ( $url );  
  104.  
  105.         $sun=explode ( '  
  106.                        ',$data );  
  107.         $sun=explode ( "  
  108.                        ",$sun[1] );  
  109.         $sun=explode ( "  
  110.                        ",$sun[1] );  
  111.         $sun=explode ( "",$sun[0] );  
  112.         $sunrise=strlen ( $sun[0] );  
  113.         $sunrise=substr ( $sun[0],$sunrise-5 );//日出时间  
  114.         $sunset=strlen ( $sun[1] );  
  115.         $sunset=substr ( $sun[1],$sunset-5 );//日落时间  
  116.         $sunhour=substr ( $sunset,0,2 )-substr ( $sunrise,0,2 );  
  117.         $sunminute=$sunhour*60+substr ( $sunset,-2 )-substr ( $sunrise,-2 );//日照时间  
  118.         $yubao=explode ( 'class="yuBaoTable"',$data );  
  119.         $num=count ( $yubao );  
  120.         $tl=array();  
  121.         $th=array();  
  122.         $fx=array();  
  123.         $fl=array();  
  124.         $weather=array();  
  125.         //第一天  
  126.         $tr=explode ( "",$yubao[1] );  
  127.         $td=explode ( "",$tr[0] );  
  128.         $weather[]=$this->getChinese ( $td[3],"UTF-8" );//晚上天气  
  129.         $fx[]=$this->getChinese ( $td[5],"UTF-8" );//晚上风向  
  130.         $fl[]=substr ( $this->getChineseNum ( $td[6],"UTF-8" ),5 );//晚上风力  
  131.         $tltemp=explode ( "",$td[4] );//最低气温  
  132.         $tl[]=$this->getNum ( $tltemp[1] );  
  133.         //从第二天到第七天  
  134.         for ( $i=2; $i<$num-1; $i++ )  
  135.         {  
  136.             $tr=explode ( "",$yubao[$i] );  
  137.             $td=explode ( "",$tr[0] );  
  138.             $weather[]=$this->getChinese ( $td[3],"UTF-8" );//白天天气  
  139.             $fx[]=$this->getChinese ( $td[5],"UTF-8" );//白天风向  
  140.             $fltemp=substr ( $this->getChineseNum ( $td[6],"UTF-8" ),5 );  
  141.             $fl[]=$fltemp;//白天风力  
  142.             $thtemp=explode ( "",$td[4] );  
  143.             $th[]=$this->getNum ( $thtemp[1] );//最高气温  
  144.             $td=explode ( "",$tr[1] );  
  145.             $tltemp=explode ( "",$td[3] );  
  146.             $tl[]=$this->getNum ( $tltemp[1] );//最低气温  
  147.  
  148.         }  
  149.         //第八天  
  150.         $tr=explode ( "",$yubao[$num-1] );  
  151.         $td=explode ( "",$tr[0] );  
  152.         $weather[]=$this->getChinese ( $td[3],"UTF-8" );//白天天气  
  153.         $fx[]=$this->getChinese ( $td[5],"UTF-8" );//白天风向  
  154.         $fl[]=substr ( $this->getChineseNum ( $td[6],"UTF-8" ),5 );//白天风力  
  155.         $thtemp=explode ( "",$td[4] );  
  156.         $th[]=$this->getNum ( $thtemp[1] );//最高气温  
  157.         if ( count ( $weather ) ==8 )  
  158.         {  
  159.             return array ( "weather"=>$weather,"tl"=>$tl,"th"=>$th,"fx"=>$fx,"fl"=>$fl,"sunset"=>$sunset,"sunrise"=>$sunrise,"sunminute"=>$sunminute );  
  160.         }  
  161.         else 
  162.         {  
  163.             return 1;  
  164.  
  165.         }  
  166.     }  
  167.     function getday1 ( $cityid )  
  168.     {  
  169.         $url="http://www.weather.com.cn/weather/".$cityid.".shtml";  
  170.         $data=$this->f->fetch ( $url );  
  171.         $yubao=explode ( 'class="yuBaoTable"',$data );  
  172.         $tr=explode ( "",$yubao[1] );  
  173.         $td=explode ( "",$tr[0] );  
  174.         $thtemp=explode ( "",$td[4] );  
  175.         return $this->getNum ( $thtemp[1] );  
  176.  
  177.     }