ホームページ  >  記事  >  バックエンド開発  >  収集演習 (1) PHP が全国の小学校を取得 (データは Tencent Friends Network から取得)

収集演習 (1) PHP が全国の小学校を取得 (データは Tencent Friends Network から取得)

WBOY
WBOYオリジナル
2016-06-23 14:37:351155ブラウズ

    注:发现腾讯朋友网已经改版,部分参数需要自己获得修改 !!!

数年前、ある州の小学校のデータを取得する必要があったのですが、friends.com で小学校を分析したところ、関連するデータが取得できることがわかりました。

湖南省郴州市宜章県のすべての小学校を取得すると

Web ページで要求されたアドレスは

http://api.pengyou.com/json.php?cb= であることがわかります。 __i_3&mod=school&act=selector&schooltype=6&country=0&province= 43&district=431022&g_tk=1964222334

ここで返されるのは json です

document.domain = "pengyou.com"; __i_3({"code":0,"subcode":0,"......});

解析後、宜章県のすべての小学校が含まれていることがわかります。 。 。

パラメータを分析しました

schooltype=6 は小学校を意味します

country = 0 は中国を意味します

province = 43 は湖南省を意味します

district = 431022 は宜章県を意味します

g_tk = 196422233 4 分かりませんが、おそらくそうです乱数

これらのパラメータを使用すると、対応する小学校を自分で取得できます。 。

湖南省郴州市のすべての郡を取得: http://api.pengyou.com/json.php?cb=__i_6&mod=getdistrict&cityid=4310&district_obj_name=_distinct&g_tk=271354436

取得するには、学校は値を取得する必要がありますしかし、対応する都道府県と地区を取得するための対応するネットワーク リクエストが見つからなかったので、ページを検索したところ、州の値が

http://cn.qzonestyle.gtimg.cn/ から取得されていることがわかりました。 Campus/js/locations.js

解決する必要がある問題:

1. location.js で州と市の ID 値を取得する場合は、正規表現を使用する必要があります

2. に基づいて郡 ID を取得します。都市 ID

3. file_get_contents が関連する学校を取得したら、対応する user_agent を取得して設定する必要があります。そうしないと、No data available が返されます。

以下は対応するコードです

header("Content-type:text/html; charset=utf-8");set_time_limit(0);$js_data = @file_get_contents("locations.js");preg_match_all("/;location_array\[([0-9]{2})?\]='([^']+)?'/",$js_data,$locations);$datas = array();if(array_filter($locations[1]) && array_filter($locations[2])){    foreach($locations[1] as $key => $val){        preg_match_all("/;sublocation_array\[".$val."\]\[([0-9]{4,})\]='([^']+)?'/", $js_data, $matches);        $datas[$val]['name']= $locations[2][$key];        foreach($matches[1] as $k =>$v){            $datas[$val]['sub'][$v] = $matches[2][$k];        }    } }function getDatas($url){    $getPageSetting = array(        'http' => array(           'timeout' => 5,           'method' => 'GET',           'protocol_version'=>'1.1',           'header' =>                    "User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_0 like Mac OS X; en-us) AppleWebKit/532.9 (KHTML, like Gecko) Version/4.0.5 Mobile/8A293 Safari/6531.22.7\r\n" .                    //"Referer: http://......php\r\n".浏览器访问过的,上一个页面的整个url地址字符串,直接在地址栏输入url访问此页面则没有此项                    "Host: isdspeed.qq.com\r\n" .//这项可以省略,如果这里设置错误会报错:failed to open stream: HTTP request failed!                     "Accept-Language: zh-cn,zh;q=0.5\r\n" .                    "Accept-Encoding: gzip, deflate\r\n" .                    "Accept-Charset: GBK,utf-8;q=0.7,*;q=0.3\r\n" .                    "Content-Type:application/x-www-form-urlencoded".                    "Accept: text/javascript, application/javascript, */*\r\n" .                    "Connection: keep-alive\r\n\r\n"        )    );    //$getHtml= file_get_contents($url, FALSE, stream_context_create($getPageSetting));   // 发现腾讯朋友网已经改版 所以直接用 file_get_contents 获得    $getHtml = file_get_contents($url); return $getHtml;    }/** * 创建文件夹 * @param string $path 文件夹路径 */function createFolder($path){    if (!file_exists($path)) {        createFolder(dirname($path));        mkdir($path, 0777);    }}$areas = array();// 获得相关省市县的小学foreach ($datas as $pid=>$rows){    foreach($rows as $k=>$v){        if($k =='sub'){            foreach($v as $cid =>$city){                $cityUrl = "http://api.pengyou.com/json.php?mod=getdistrict&cityid=".$cid."&district_obj_name=_distinct&g_tk=1523170442";                $result = getDatas($cityUrl);                $districtIds = json_decode($result,true);                $areas[$pid][$cid] = $districtIds['result']['district_arr'];                $district_arr= $districtIds['result']['district_arr'];                foreach($district_arr as $did =>$district){                    $url = "http://api.pengyou.com/json.php?&mod=school&act=selector&schooltype=6&country=0&province=".$pid."&district=".$did."&g_tk=1523170442";                    $schools = getDatas($url);                    $schools = json_decode($schools,true);                    $school_data = str_replace("&middot;","\r\n",strip_tags($schools['result']));                    $dirs = "school/".iconv('utf-8', 'gbk', $rows['name'])."/".iconv('utf-8', 'gbk', $city);                    createFolder($dirs);                    @file_put_contents($dirs.'/'.iconv('utf-8', 'gbk', $district).'.txt', $school_data);                }            }                    }            }}echo '<pre class="brush:php;toolbar:false">';print_r($areas);

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。