The collected data will be used in the project, so I tried it on the CSDN blog first. The Simple HTML DOM (official website) library is used here, which can conveniently traverse HTML documents.
<?php include_once('simple_html_dom.php'); header('Content-Type:text/html;charset=utf-8'); $html = file_get_html('http://blog.csdn.net/szy361'); $res = $html->find('#hotarticls ul.panel_body li a[title]');//取得id=hotarticls下class为panel_bodya的ul标签下的a的title $span = $html->find('#hotarticls ul.panel_body li span');//取得span foreach($res as $element){ $arr[] = $element->title.'+'.$element->href;//将title值和href的值通过+连起来 } foreach($span as $e){ $brr[] = $e->innertext;//得到span下的值组成的数组 } //将两个数组组成一个新的二维数组 for($i=0;$i<count($res);$i++){ $crr[] = explode('+',$arr[$i]); $crr[$i][] = $brr[$i]; } return $crr;
Extension:
Getting started with PHP Simple HTML DOM parser