使用框架
Laravel
使用扩展
php-simple-html-dom-parser
附上扩展连接 https://github.com/sunra/php-simple-html-dom-parser
使用说明
修改simple_htmldom.php
文件里file_get_html()
方法参数$offset=null
和$maxLen=null
不然报错[file_get_contents(): stream does not support seeking PHP](https://stackoverflow.com/questions/40816714/file-get-contents-stream-does-not-support-seeking-php)
上代码
public function index(){ $url = 'https://www.taobao.com/tbhome/page/market-list?spm=a21bo.50862.201867-main.1.SkuZyi'; $dom = HtmlDomParser::file_get_html ($url); $level1 = $dom->find(".home-category-list"); foreach ($level1 as $v) { //多级类目 均按照类选择器获取节点。 foreach ($v->find('.category-name-level1') as $k){ $res = TaoBaoModel::create([ 'name' => $k->text(), 'pid' => 0, 'level' =>1, ]); } $level2 = $v->find('.category-list-item'); foreach ($level2 as $l){ $level2_res = TaoBaoModel::create([ 'name' => $l->first_child()->text(), 'pid' => $res->id, 'level' =>2, ]); $level3 = $l->find('.category-items'); foreach ($level3 as $p) { $level4 = $p->children(); foreach ($level4 as $o) { TaoBaoModel::create([ 'name' => $o->text(), 'pid' => $level2_res->id, 'level' =>3, ]); } } } } }