PHP+正则抓取淘宝装修市场设计师数据

目的

竞争对手分析,了解淘宝装修市场上线了多少无线模板设计师

步骤

  1. 找到目标页面

https://zxn.taobao.com/wei_template_list.htm?p=1

https://zxn.taobao.com/wei_template_list.htm?p=870

  1. 取出html源文件
$html = file_get_contents("https://zxn.taobao.com/wei_template_list.htm?p=$i");
  1. 利用正则来解析html
	$start = "\/\/zxn.taobao.com\/designer_detail\.htm\?designerId=";
	$end = "\">";
	preg_match_all("/$start(\d+)$end/", $html, $matches);

取matches[1]得到设计师的id号

array(20) {
  [0]=>
  string(7) "1038354"
  [1]=>
  string(7) "1029480"
  [2]=>
  string(7) "1036438"
  [3]=>
  string(5) "14040"
  ...
  [18]=>
  string(7) "1033337"
  [19]=>
  string(5) "75477"
}
  1. 遍历分页迭加得所有的设计id号数组

4.1 先声明一个空数组用来不加的merge用

$designer_id_array = [];

4.2 然后在循环体中合并数组

for ($i = 1; $i <= $page ; $i++) {
	$designer_id_array = array_merge($designer_id_array, $matches[1]);
}

4.3 让$page=2验证结果


array(40) {
  [0]=>
  string(7) "1038354"
  [1]=>
  string(7) "1029480"
  [2]=>
  string(7) "1036438"
  [39]=>
  string(5) "40890"
}

  1. 对数组元素去重复
array_unique($designer_id_array)
  1. sizeof()求数组长度

echo '无线设计师人数为:' . sizeof($designer_id_array);

大功告成

代码没多讲究,只为实现我自己要的功能。

下载地址:https://git.oschina.net/laeser/spider

转载于:https://my.oschina.net/huangxiujie/blog/912026

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值