美文网首页
获取省市县三级数据

获取省市县三级数据

作者: 苍老师的眼泪 | 来源:发表于2022-05-12 22:48 被阅读0次
    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use App\Area;
    
    class AreaCtrl extends Controller
    {
        static $base_link = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/';
    
        static public function example()
        {
            $url = 'http://www.stats.gov.cn/tjsj/tjbz/tjyqhdmhcxhfdm/2021/11/1101.html';
            file_put_contents(__DIR__ . '/example.html', file_get_contents($url));
        }
        
        static public function get_base_link($link)
        {
            return substr($link, 0, strripos($link, '/')) . '/';  
        }
    
        static public function country() 
        {
            Area::truncate();
    
            $start_url = 'index.html';
    
            $target_link = self::$base_link . $start_url;
    
            $content = file_get_contents($target_link);
            // $content = file_get_contents(__DIR__ . '/example.html');
    
    
            $regexp = '#<td><a href="(.+?)">(.+?)<br.+?</td>#s';
    
            preg_match_all($regexp, $content, $result); 
    
            $province_detail_links = $result[1];
            $province_names = $result[2];
    
    
            $province_count = count($province_detail_links);
    
            for($province_index = 0; $province_index < $province_count; $province_index++) {
                $province_name = $province_names[$province_index];
    
                logger('正在爬 ' . $province_name . ' 的数据:' . $province_index);
    
                $row = Area::create([
                    'level' => 1,
                    'code' => null,
                    'name' => $province_name,
                    'pid' => 0,
                ]);
                self::province($row->id, self::get_base_link($target_link) . $province_detail_links[$province_index], $province_name);
                
    
            }
    
        }
    
        // 获取某个省下面的市区
        static public function province($pid = null, $link = null, $name = null) 
        {
    
            $content = file_get_contents($link);
    
    
            // $content = file_get_contents(__DIR__ . '/example.html');
    
            $regexp = '#<tr.+?="citytr">.+?href="(.+?)">(.+?)</a>.+?href=".+?">(.+?)</a>.+?</tr>#s';
    
            preg_match_all($regexp, $content, $result); 
    
            $city_detail_links = $result[1];
            $city_codes = $result[2];
            $city_names = $result[3];
            
            
            $city_count = count($city_detail_links);
            for($city_index = 0; $city_index < $city_count; $city_index++) {
                
                $city_detail_link = $city_detail_links[$city_index];
                $city_code = $city_codes[$city_index];
                $city_name = $city_names[$city_index];
    
                $row = Area::create([
                    'level' => 2,
                    'code' => $city_code,
                    'name' => $city_name,
                    'pid' => $pid,
                ]);
    
                self::city($row->id, self::get_base_link($link) . $city_detail_link, $city_name);
    
            }
    
        }
    
        // 获取某个市下面的区县
        static public function city($pid = null, $link = null, $name = null)
        {
    
            $content = file_get_contents($link);
    
    
            // file_put_contents(__DIR__ . '/example.html', $content);
    
            // $content = file_get_contents(__DIR__ . '/example.html');
    
    
            $regexp = '#<tr.+?"countytr">.+?href="(.+?)">(.+?)</a></td>.+?href=".+?">(.+?)</a>.+?</tr>#s';
    
    
            preg_match_all($regexp, $content, $result); 
    
            $county_detail_links = $result[1];
            $county_codes = $result[2];
            $county_names = $result[3];
    
    
            $county_count = count($county_detail_links);
    
            for($county_index = 0; $county_index < $county_count; $county_index++) {
    
                $county_detail_link = $county_detail_links[$county_index];
                $county_code = $county_codes[$county_index];
                $county_name = $county_names[$county_index];
    
                $row = Area::create([
                    'level' => 3,
                    'code' => $county_code,
                    'name' => $county_name,
                    'pid' => $pid,
                ]);
    
            }
        }
    
        static public function process() 
        {
            $all_province = Area::where('pid', 0)->get();
    
            foreach ($all_province as $item) {
                $first_child = Area::where('pid', $item->id)->first();
    
                $code = intval($first_child->code / 10000000000) * 10000000000;
                
                $item->update(['code' => $code]);
    
            }
    
        }
    
        static public function process1()
        {
            $all_province = Area::all();
    
            foreach($all_province as $item) {
                $item->update(['code' => $item->code / 1000000]);
            }
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:获取省市县三级数据

          本文链接:https://www.haomeiwen.com/subject/pgoeurtx.html