echo "==================start=======================<br />";
// 1、获取网页
$path = THIS_PATH . "download";
$url = "http://10.maigoo.com/list_1187.html";
$pathinfo = pathinfo($url);
$html_pathname = $path . DS;
$html_filename = $html_pathname . "list_1187.htm";
if (!file_exists($html_filename)) {
$text = curl_get_file_contents($url);
save_file($html_filename, $text);
} else {
$text = file_get_contents($html_filename);
}
// 2、获取区域
//start pos
$start = ‘<div class="b-brand-nlist hoverdetail">‘;
//end pos
$end = ‘<div id="copyright">‘;
$pos_start = strpos($text, $start);
$pos_end = strpos($text, $end, $pos_start);
$pos_end += strlen($end);
$content = substr($text, $pos_start, $pos_end-$pos_start);
save_file($html_pathname."list_1187.html", $content);
// 3、获取所有的一级
$pattern = ‘@<div class="aclist">.*<div class="clear"></div>@Usi‘;
if (!preg_match_all($pattern, $content, $matches)) {
die("===============not match anything===================<");
}
echo "=========================================<br />";
$index = 0;
foreach ($matches[0] as $pinpai_cate) {
save_file($html_pathname. $index . ".html", $pinpai_cate);
// 获得一级分类 url 和 name
get_level1_url_and_name($pinpai_cate, $cate1_url, $cate1_name);
// echo "==================$一个品牌=======================<br />";
$pattern = ‘@<li addbg="#400143".*</li>@Usi‘;
if (preg_match_all($pattern, $content, $matches)) {
foreach($matches[0] as $one_brand);
}
}
echo "==================end=======================<br />";
}
基本原理就是先获取下载网页到本地,然后截取,最后用正则匹配。自己做得过程中没有对代码进行调优,导致代码太长,重复的地方太多,若截取的地方用正则还是无法判断,或者说区域有很多重复点,就需要再次截取接着排除干扰,比较繁琐,另外需要多写函数,把所有代码优化之后才能更深入提高自己水平。