当前位置:首页 > 技术文档 > 正文内容

抓取最近八天天气(非利用接口,直接从网站提取)

admin10年前 (2015-05-11)技术文档1743
<?php
   /*
    *特别注意,第一天没有最高气温数据,第八天没有最低气温数据
    *注意对数字进行过滤时不要忘记对负号进行判断
    *对风力过滤时要考虑到3-5级这种格式
    *http://blog.qita.in
   class weatherfetch{
       private $f;
       function getNum($string) {
           $tmpstr = '';
           $strlen = strlen($string);
           for($i=0; $i<$strlen; $i++) {
                $str=substr($string, $i, 1);
                $str1=trim($str);
                if(is_numeric($str1)){
                    $tmpstr.=$str1+0;
  
                }
                if($str1=="-"&&is_numeric(substr($string, $i-1, 1))){
                $tmpstr.= $str1;
            }
  
            }
           return $tmpstr;
        }
        function __construct(){
          $this->f= new SaeFetchurl();
  
        }
        function getChineseNum($string){
           $tmpstr = '';
           $arr = array(1,2,3,4,5,6,7,8,9,0);
          $strlen = strlen($string);
          for($i=0; $i<$strlen; $i++) {      
  
           $str=substr($string, $i, 1);
  
           $str1=trim($str);
           if( ord($str)>0xA0 ){
  
            $tmpstr.= substr($string, $i, 3);
  
            $i = $i+2;
  
           }
  
            if(is_numeric($str1)){
  
                $tmpstr.= $str1;
  
            }
            if($str1=="-"&&is_numeric(substr($string, $i-1, 1))&&is_numeric(substr($string, $i+1, 1))){
                $tmpstr.= $str1;
            }
  
          }
  
                return $tmpstr;
  
        }
        function getChinese($string,$encode="GBK") {
               switch($encode){
                    case "GBK" :$codelength=2;break;
               case "GB2312" :$codelength=3;break;
               case "UTF-8" :$codelength=3;break;
                   case "UTF-16" :$codelength=4;break;
  
               }
               $tmpstr = '';
               $arr = array(1,2,3,4,5,6,7,8,9,0);
               $strlen = strlen($string);
               for($i=0; $i<$strlen; $i++) {
                    $str=substr($string, $i, 1);
                    $str1=trim($str);
                    if( ord($str)>0xA0 ){
                    $tmpstr.= substr($string, $i, $codelength);
                    $i = $i+$codelength-1;
                    }
  
                }
            return $tmpstr;
        }
       function get($cityid){
           $url="http://www.weather.com.cn/weather/".$cityid.".shtml";
           $data=$this->f->fetch($url);
  
           $sun=explode('<div class="weatherTopright">',$data);
           $sun=explode("<dl>",$sun[1]);
           $sun=explode("</dl>",$sun[1]);
           $sun=explode("</strong>",$sun[0]);
           $sunrise=strlen($sun[0]);
           $sunrise=substr($sun[0],$sunrise-5);//日出时间
           $sunset=strlen($sun[1]);
           $sunset=substr($sun[1],$sunset-5);//日落时间
           $sunhour=substr($sunset,0,2)-substr($sunrise,0,2);
           $sunminute=$sunhour*60+substr($sunset,-2)-substr($sunrise,-2);//日照时间
           $yubao=explode('class="yuBaoTable"',$data);
           $num=count($yubao);
           $tl=array();
           $th=array();
           $fx=array();
           $fl=array();
           $weather=array();
           //第一天
           $tr=explode("</tr>",$yubao[1]);
           $td=explode("</td>",$tr[0]);
           $weather[]=$this->getChinese($td[3],"UTF-8");//晚上天气
           $fx[]=$this->getChinese($td[5],"UTF-8");//晚上风向
           $fl[]=substr($this->getChineseNum($td[6],"UTF-8"),5);//晚上风力
           $tltemp=explode("<strong>",$td[4]);//最低气温
           $tl[]=$this->getNum($tltemp[1]);
           //从第二天到第七天
           for($i=2;$i<$num-1;$i++){
               $tr=explode("</tr>",$yubao[$i]);
               $td=explode("</td>",$tr[0]);
               $weather[]=$this->getChinese($td[3],"UTF-8");//白天天气
               $fx[]=$this->getChinese($td[5],"UTF-8");//白天风向
               $fltemp=substr($this->getChineseNum($td[6],"UTF-8"),5);
               $fl[]=$fltemp;//白天风力
               $thtemp=explode("<strong>",$td[4]);
               $th[]=$this->getNum($thtemp[1]);//最高气温
               $td=explode("</td>",$tr[1]);
               $tltemp=explode("<strong>",$td[3]);
               $tl[]=$this->getNum($tltemp[1]);//最低气温
  
            }
            //第八天
            $tr=explode("</tr>",$yubao[$num-1]);
            $td=explode("</td>",$tr[0]);
            $weather[]=$this->getChinese($td[3],"UTF-8");//白天天气
            $fx[]=$this->getChinese($td[5],"UTF-8");//白天风向
            $fl[]=substr($this->getChineseNum($td[6],"UTF-8"),5);//白天风力
            $thtemp=explode("<strong>",$td[4]);
            $th[]=$this->getNum($thtemp[1]);//最高气温
            if(count($weather)==8){
                return array("weather"=>$weather,"tl"=>$tl,"th"=>$th,"fx"=>$fx,"fl"=>$fl,"sunset"=>$sunset,"sunrise"=>$sunrise,"sunminute"=>$sunminute);
            }else{
              return 1;
  
            }
        }
        function getday1($cityid){
             $url="http://www.weather.com.cn/weather/".$cityid.".shtml";
             $data=$this->f->fetch($url);
             $yubao=explode('class="yuBaoTable"',$data);
             $tr=explode("</tr>",$yubao[1]);
             $td=explode("</td>",$tr[0]);
             $thtemp=explode("<strong>",$td[4]);
             return $this->getNum($thtemp[1]);
  
        }
    }


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:https://yuyunhe.cn/index.php/post/38.html

分享给朋友:

“抓取最近八天天气(非利用接口,直接从网站提取)” 的相关文章

如何快速去除代码前的行号

如何快速去除代码前的行号...

php无限分类-总结

//php代码部分  <?php         header('Content-Type:text/html;charset=utf-8');    ...

phpcms v9更换模板的具体操作方法

phpcms v9更换模板的具体操作方法

这篇文章主要介绍了phpcms v9更换模板的具体操作方法,需要的朋友可以参考下分享一下Phpcms V9更换模板的具体操作方法先分享下大概的步骤:1、上传模版文件到服务器;2、在站点管理 里边【模板风格配置】选择新模板;3、设置不同模型对应模板;4、修改现有的栏目,匹配新模板;5、更新栏目缓存、系...

推荐一些国内的Jquery CDN免费服务

Jquery是个非常流行的JS前端框架,在很多网站都能看到它的身影。很多网站都喜欢采用一些Jquery CDN加速服务,这样网站加载jquery会更快。之前火端网络的一些网站都是使用Google的jquery CDN,如:http://ajax.googleapis.com/ajax/lib...

Discuz!二次开发必备知识——DZ程序文件目录含义

分类: DZ程序文件目录含义基于7.0的标准程序,部分与插件无关的文件不作说明文件颜色说明:红色:程序核心文件,修改这类文件时千万要注意安全!橙色:做插件几乎不会用到的文件,大概了解功能就可以了,其实我也不推荐修改这些文件绿色:函数类文件,许多功能强大的自定义函数可以调用这类文件来...

TP 查询缓存

<?php     function cache_data($sql){             $SqlDao&nbs...

发表评论

访客

看不清,换一张

◎欢迎参与讨论,请在这里发表您的看法和观点。