[PHP] php抓取天气预报(天气,气温,风力,日出日落...) →→→→→进入此内容的聊天室

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

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

这儿你可以回复上面这条便签

captcha