• PHP爬取历史天气


    PHP爬取历史天气

    PHP作为宇宙第一语言,爬虫也是非常方便,这里爬取的是从天气网获得中国城市历史天气统计结果。

    程序架构

    main.php

    <?php
        include_once("./parser.php");
        include_once("./storer.php");
        #解析器和存储器见下文
        $parser = new parser();
        $storer = new storer();
        #获得url列表
        $urlList = $parser->getCityList("http://lishi.tianqi.com/");
        #依次解析新的URL网站内容,并存到数据库中
        foreach($urlList as $url)
        {
            $data = $parser->getData($url);
            $storer->store($data);
        }
    

    解析器

    解析器提供两个接口,一个是解析主页,获得url列表;另一个是解析每座城市的数据,获得该城市的历史天气数据。

    这里使用到的解析库是phpquery,使用JQuery的查询方式,简单高效。

    <?php
        #借助JQuery库解析
        include_once("./phpQuery-onefile.php");
    class parser
    {
        //获取城市url列表
        function getCityList($url)
        {
            //直接在线流下载
            phpQuery::newDocumentFile($url);
            //第一次选择
            $links = pq(".bcity *");
            $urlList = [];
            foreach ($links as $link) {
                #第二次选择
                $tmp = pq($link)->find('a')->attr('href');
                #过滤组标签
                if ($tmp!="#" and $tmp!="") {
                    #检查url
                    if(strpos($tmp,"-")==false and filter_var($tmp, FILTER_VALIDATE_URL))
                        $urlList[] = $tmp; #添加URL列表
                }
            }
            return $urlList;
        }
            
        //获取某个城市的历史气候
        function getData($url)
        {
            //直接在线流下载
            phpQuery::newDocumentFile($url);
            //第一次选择
            $text = pq("div .tqtongji p")->text();
                
            #匹配城市
            $city = $this->match("/,(.+)共出现/",$text);
            #匹配天气
            $rainy = $this->match("/雨(d+)天/",$text);
            $cloudy = $this->match("/多云(d+)天/",$text);
            $sunny = $this->match("/晴(d+)天/",$text);
            $overcast = $this->match("/阴(d+)天/",$text); #为了跟cloudy区分
            $snowy = $this->match("/雪(d+)天/",$text);
            #匹配拼音
            $pinYin = $this->match("/http://lishi.tianqi.com/(.*?)/index.html/",$url);
            
            $result["url"] = $url;
            $result["city"] = $city;
            $result["pinYin"] =  $pinYin;
            $result["rainy"] = $rainy;
            $result["cloudy"] = $cloudy;
            $result["sunny"] = $sunny;
            $result["overcast"] = $overcast;
            $result["snowy"] = $snowy;
            return $result;
        }
        #正则解析
        function match($rule,$text)
        {
            preg_match_all($rule, $text, $result);
            #有些地区不是所有天气都有
            if(count($result[1])==0)
                return "0";
            return $result[1][0];
        }
    }
    

    存储器

    使用MySQLi接口即可,代码如下:

    <?php
        class storer
        {
            public $mysqli;
            function __construct()
            {
                $this->mysqli = new mysqli('localhost', '***', '******', 'phpWeather');
                $this->mysqli->query("SET NAMES UTF8");
            }
    
            function store($data)
            {
                $url = $data["url"];
                $city = $data["city"];
                $pinYin = $data["pinYin"];
                $rainy = $data["rainy"];
                $cloudy = $data["cloudy"];
                $sunny = $data["sunny"];
                $overcast = $data["overcast"];
                $snowy = $data["snowy"];
                
                #字符串在插入时要添加''来区分
                $insertData = "VALUES('$city','$pinYin',$rainy,$cloudy,$sunny,$overcast,$snowy,'$url');";
                #sql分开写更加清楚
                $sql = "INSERT INTO record(city,pinYin,rainy,cloudy,sunny,overcast,snowy,url)".$insertData;
                $isok = $this->mysqli->query($sql);
                if($isok)
                {
                    echo "$city 数据添加成功
    ";
                }
                else
                {
                    echo $sql . "
    ";
                    echo "$city 数据添加失败
    ";
                }
            }
            function __destruct()
            {
                $this->mysqli->close();
            }
        }
    ?>
    

    爬虫结果

    共爬取了3119座城市的从2011年到现在的历史天气,接下来的数据分析以及可视化留到下一篇博客讲述。

  • 相关阅读:
    小米官网中,鼠标移动到某个卡片,会轻轻上浮
    微信小程序之表单验证rule
    小程序调用自定义组件里的方法(this.selectComponent(#id))
    Git —— GitLab生产ssh密钥
    WX获取地理位置列表接口示例
    for...of语句在可迭代对象(包括 Array,Map,Set,String,TypedArray,arguments 对象等等)上创建一个迭代循环,调用自定义迭代钩子,并为每个不同属性的值执行语句(return break container可终止的循环)
    规格弹出框
    //将手机号中间4位数变成*
    //时间戳转换成日期时间(年月日)
    检查手机号是否正确
  • 原文地址:https://www.cnblogs.com/fanghao/p/7496469.html
Copyright © 2020-2023  润新知