3-21 2,478 views
步骤
-
使用composer安装 php抓取扩展包
composer require jaeger/querylist
-
- 经过分析发现区域和地铁数据在类名.clearfix.filterList下面,所以代码可以这么些:
<?php
require_once 'vendor/autoload.php';
$url = 'http://www.ziroom.com/z/nl/z3.html';
$ql = \QL\QueryList::get($url);
//地区
$data = $ql->find('.clearfix.filterList:eq(0)>li .tag a')->texts();
$area_son = [];
$area_parent = [];
$area_p_key = '';
foreach ($data as $k => $v) {
if ($v == '全部'){
$area_parent[] = $data[$k-1];
}
}
foreach ($data as $v) {
if (in_array($v,$area_parent)){
$area_p_key = $v;
}
if ($v != '全部' && !in_array($v,$area_parent)){
$area_son[$area_p_key][] = $v;
}
}
//地铁
$data = $ql->find('.clearfix.filterList:eq(1)>li .tag a')->texts();
$subway_son = [];
$subway_parent = [];
$subway_p_key = '';
foreach ($data as $k => $v) {
if ($v == '全部'){
$subway_parent[] = $data[$k-1];
}
}
foreach ($data as $v) {
if (in_array($v,$subway_parent)){
$subway_p_key = $v;
}
if ($v != '全部' && !in_array($v,$subway_parent)){
$subway_son[$subway_p_key][] = $v;
}
}
var_dump($area_parent);
var_dump($subway_parent);
// p($area_son);
// p($subway_son);
- 得到如下结果
array (size=13)
0 => string '东城' (length=6)
1 => string '西城' (length=6)
2 => string '朝阳' (length=6)
3 => string '海淀' (length=6)
4 => string '丰台' (length=6)
5 => string '石景山' (length=9)
6 => string '通州' (length=6)
7 => string '昌平' (length=6)
8 => string '大兴' (length=6)
9 => string '顺义' (length=6)
10 => string '房山' (length=6)
11 => string '门头沟' (length=9)
12 => string '亦庄开发区' (length=15)
H:\wamp\www\projects\test\test.php:51:
array (size=21)
0 => string '1号线' (length=7)
1 => string '2号线' (length=7)
2 => string '4号线' (length=7)
3 => string '5号线' (length=7)
4 => string '6号线' (length=7)
5 => string '7号线' (length=7)
6 => string '8号线' (length=7)
7 => string '9号线' (length=7)
8 => string '10号线' (length=8)
9 => string '13号线' (length=8)
10 => string '14号线' (length=8)
11 => string '15号线' (length=8)
12 => string '西郊线' (length=9)
13 => string 'S1号线' (length=8)
14 => string '燕房线' (length=9)
15 => string '16号线' (length=8)
16 => string '八通线' (length=9)
17 => string '昌平线' (length=9)
18 => string '亦庄线' (length=9)
19 => string '房山线' (length=9)
20 => string '机场线' (length=9)
Array
(
[东城] => Array
(
[0] => 安定门
[1] => 安贞
[2] => 崇文门
[3] => 朝阳门内
[4] => 东花市
[5] => 地安门
[6] => 灯市口
[7] => 东四
[8] => 东单
[9] => 东直门
[10] => 广渠门
[11] => 和平里
[12] => 交道口
[13] => 金宝街
[14] => 建国门内
[15] => 天坛
[16] => 西罗园
[17] => 永定门
[18] => 左安门
[19] => 北新桥
[20] => 雍和宫
)
[西城] => Array
(
[0] => 白纸坊
[1] => 长椿街
[2] => 车公庄
[3] => 德胜门
[4] => 阜成门
[5] => 广安门
[6] => 官园
[15号线] => Array
(
[0] => 俸伯
[1] => 顺义
[2] => 石门
[3] => 南法信
[4] => 后沙峪
[5] => 花梨坎
[6] => 国展
[7] => 孙河
[8] => 马泉营
[9] => 崔各庄
[10] => 望京东
[11] => 望京
[12] => 望京西
[13] => 关庄
[14] => 大屯路东
[15] => 安立路
[16] => 奥林匹克公园
[17] => 北沙滩
[18] => 六道口
[19] => 清华东路西口
)
[西郊线] => Array
(
[0] => 巴沟
[1] => 万安
[2] => 茶棚
[3] => 颐和园西门
)
[S1号线] => Array
(
[0] => 苹果园