PHP 之simple_html_dom实现网页数据采集
<?php set_time_limit(0); include './simple_html_dom.php'; $url = 'https://price.pcauto.com.cn/price/rank/nb1-lb0-o1/'; $html = file_get_html($url); $list = $html->find("#JlistTb .j-list"); $id = array(); $title = array(); $price = array(); $dealer_price = array(); $pic = array(); for ($i=0; $i < count($list); $i++) { $idText = $html->find("#JlistTb .j-list dd[name=dealer_price]", $i)->id; $id[] = explode("_", $idText)[2]; $title[] = $html->find("#JlistTb .j-list .sname", $i)->innertext; $price[] = $html->find("#JlistTb .j-list .pri", $i)->innertext; $dealer_price[] = $html->find("#JlistTb .j-list .emphs", $i)->innertext; $pic[] = "https:".$html->find("#JlistTb .j-list .pic img", $i)->getAttribute("#src"); } $list = array(); for ($i=0; $i < count($title); $i++) { $list[$i] = array( 'id' => $id[$i], 'title' => $title[$i], 'price' => $price[$i], 'dealer_price' => $dealer_price[$i], 'pic' => $pic[$i], 'area_id' => 1 ); } var_dump($list); ?>
数组打印结果为