搜索
首页php教程PHP源码抓取最近八天天气(非利用接口,直接从网站提取)

php代码

<?php
   /*
    *特别注意,第一天没有最高气温数据,第八天没有最低气温数据
    *注意对数字进行过滤时不要忘记对负号进行判断
    *对风力过滤时要考虑到3-5级这种格式
    */
   class weatherfetch{
       private $f;
       function getNum($string) {
           $tmpstr = &#39;&#39;;
           $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 = &#39;&#39;;
           $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 = &#39;&#39;;
               $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(&#39;<div class="weatherTopright">&#39;,$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(&#39;class="yuBaoTable"&#39;,$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(&#39;class="yuBaoTable"&#39;,$data);
             $tr=explode("</tr>",$yubao[1]);
             $td=explode("</td>",$tr[0]);
             $thtemp=explode("<strong>",$td[4]);
             return $this->getNum($thtemp[1]);
  
        }
    }
声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript开发工具

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

螳螂BT

螳螂BT

Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境