<?php
header('content-type:text/html;charset=utf-8');
//采集数据
$url="http://www.keepclub.com/club/environ/";
if(file_exists('aa.txt')){
$data=file_get_contents('aa.txt');
}else{
$data=file_get_contents($url);
file_put_contents('aa.text', $data);
}
$arr='#<div class="hd_list">(.*)</div>#isU';
preg_match_all($arr, $data, $src);
$arr2='#<li.*><img src="(.*)" alt=""/></li>#isU';
preg_match_all($arr2, $src[1][0], $src2);
//连接数据库
$dsn = "mysql:host=localhost;dbname=数据库名称";
$db = new PDO($dsn, '数据库账号', '数据库密码');
$db->query("set names utf8");
$path=dirname($_SERVER['SCRIPT_NAME']);//根目录
$arry=array();
foreach ($src2[1] as $k => $img) {
$paths=pathinfo($img);
$newimg='img/'."club".$k.'.'.$paths['extension'];
file_put_contents($newimg,file_get_contents($img));
$Paths=$path.'/'.$newimg;
array_push($arry,"(null,null,'$Paths')");
}
$count = $db->exec("insert into 表名 values".implode(',',$arry));
if($count){
echo "成功";
}else{
echo "失败";
}
?>