simple.html.php网络上有这个文件直接下载引用即可
-------------------------实际运行的php
<?php
header("Content-type:text/html;charset=GBK");
error_reporting(E_ALL^E_NOTICE);
require_once 'simple_html_dom.php';
$cookie_file= tempnam('./temp','cookie');
$kill = get('https://list.tmall.com/search_product.htm?spm=a220m.1000858.0.0.hFwP0u&s=60&q=%B2%A3%C4%F2%CB%E1&sort=s&style=g&from=mallfp..pc_1_searchbutton&active=2&type=pc#J_Filter'/*,false,1,false,$cookie_file*/);
$html = str_replace('data-ks-lazyload','src',$kill);
$dom = new simple_html_dom();
$dom->load($html);
$ret = $dom->getElementById("J_ItemList");
$html = $ret->innertext;
//preg_match_all("/<div class=\"product\"(.*?)>(.*?)class=\"product\"/is", $html, $matched);
$matched = preg_split('/<div class=\"product\"(.*?)>/is', $html);// 内部扒一层
$matched = array_filter($matched);
for($i=1;$i< count($matched);$i++){
$matcheding = preg_split('/<div class=\"productImg-wrap\">/is', $matched[$i]);// 2次扒一层
// 图片中的src
$patternImg = "/<img.*src\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/";
preg_match($patternImg, $matcheding[1] ,$match);
$z[$i-1]['img'] = $match[1];
// 商品价格
$patternPrize = "/title\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/";
preg_match($patternPrize, $matcheding[1] ,$matchprize);
$z[$i-1]['prize'] = $matchprize[1];
/*
// 商品名字
$patternPrize = "/.*title\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/";
preg_match($patternPrize, $matcheding[1] ,$matchprize);
$z[$i-1]['title'] = $matchprize[1];*/
// 天猫网点击之后进入详情页链接
/* $patterntmallurl = "/<a.*href\s*=\s*[\"|\']?\s*([^>\"\'\s]*)/";
preg_match($patterntmallurl, $matcheding[1] ,$matchprize);
$z[$i-1]['tmallurl'] = $matchprize[1];*/
// 商品的名字
preg_match_all('/[^<>]+(?=<\/a>)/is',$matcheding[1],$descrArr);
$z[$i-1]['title'] = $descrArr[0][2];
$z[$i-1]['name'] = $descrArr[0][1];
$z[$i-1]['datafromshop'] = $descrArr[0][3];
// 进行图片下载
imgdown('http:'.$match[1],$z[$i-1]['name']);
}
function get($url,$head=false,$foll=1,$ref=false,$cookie_file =null){
$curl = curl_init(); // 启动一个CURL会话
if($head){
curl_setopt($curl,CURLOPT_HTTPHEADER,$head);//模似请求头
}
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION,$foll); // 使用自动跳转
if($ref){
curl_setopt($curl, CURLOPT_REFERER, $ref);//带来的Referer
}else{
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
}
curl_setopt($curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
/* curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_file); // 存放Cookie信息的文件名称
curl_setopt($curl, CURLOPT_COOKIEFILE,$cookie_file); // 读取上面所储存的Cookie信息*/
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');//解释gzip
//curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
curl_close($curl); // 关键CURL会话
return $tmpInfo;
}
// 图片下载
function imgdown($url,$name){
$header = array("Connection: Keep-Alive", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Pragma: no-cache", "Accept-Language: zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3", "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:29.0) Gecko/20100101 Firefox/29.0");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, $v);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
$content = curl_exec($ch);
$curlinfo = curl_getinfo($ch);
//关闭连接
curl_close($ch);
if ($curlinfo['http_code'] == 200) {
if ($curlinfo['content_type'] == 'image/jpeg') {
$exf = '.jpg';
} else if ($curlinfo['content_type'] == 'image/png') {
$exf = '.png';
} else if ($curlinfo['content_type'] == 'image/gif') {
$exf = '.gif';
}
$name = str_replace("/","",$name);
$name = strFilter($name);// 过滤名字中的特殊字符
//存放图片的路径及图片名称 *****这里注意 你的文件夹是否有创建文件的权限 chomd -R 777 mywenjian
$filename ='imgs/'.date("YmdHis"). uniqid() .$name. $exf;//这里默认是当前文件夹,可以加路径的 可以改为$filepath = '../'.$filename
$res = file_put_contents($filename, $content);//同样这里就可以改为$res = file_put_contents($filepath, $content);
}
}
function strFilter($str){
$str = str_replace('`', '', $str);
$str = str_replace('·', '', $str);
$str = str_replace('~', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('!', '', $str);
$str = str_replace('@', '', $str);
$str = str_replace('#', '', $str);
$str = str_replace('$', '', $str);
$str = str_replace('¥', '', $str);
$str = str_replace('%', '', $str);
$str = str_replace('^', '', $str);
$str = str_replace('……', '', $str);
$str = str_replace('&', '', $str);
$str = str_replace('*', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('(', '', $str);
$str = str_replace(')', '', $str);
$str = str_replace('-', '', $str);
$str = str_replace('_', '', $str);
$str = str_replace('——', '', $str);
$str = str_replace('+', '', $str);
$str = str_replace('=', '', $str);
$str = str_replace('|', '', $str);
$str = str_replace('\\', '', $str);
$str = str_replace('[', '', $str);
$str = str_replace(']', '', $str);
$str = str_replace('【', '', $str);
$str = str_replace('】', '', $str);
$str = str_replace('{', '', $str);
$str = str_replace('}', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(';', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace(':', '', $str);
$str = str_replace('\'', '', $str);
$str = str_replace('"', '', $str);
$str = str_replace('“', '', $str);
$str = str_replace('”', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace(',', '', $str);
$str = str_replace('<', '', $str);
$str = str_replace('>', '', $str);
$str = str_replace('《', '', $str);
$str = str_replace('》', '', $str);
$str = str_replace('.', '', $str);
$str = str_replace('。', '', $str);
$str = str_replace('/', '', $str);
$str = str_replace('、', '', $str);
$str = str_replace('?', '', $str);
$str = str_replace('?', '', $str);
return trim($str);
}
//模拟登录
function login_post($url, $cookie, $post) {
$curl = curl_init();//初始化curl模块
curl_setopt($curl, CURLOPT_URL, $url);//登录提交的地址
curl_setopt($curl, CURLOPT_HEADER, 0);//是否显示头信息
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 0);//是否自动显示返回的信息
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); //设置Cookie信息保存在指定的文件中
curl_setopt($curl, CURLOPT_POST, 1);//post方式提交
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));//要提交的信息
curl_exec($curl);//执行cURL
curl_close($curl);//关闭cURL资源,并且释放系统资源
}
function post($url,$head=false,$foll=1,$ref=false,$post){
$curl = curl_init(); // 启动一个CURL会话
if($head){
curl_setopt($curl,CURLOPT_HTTPHEADER,$head);//模似请求头
}
curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
@curl_setopt($curl, CURLOPT_FOLLOWLOCATION,$foll); // 使用自动跳转
if($ref){
curl_setopt($curl, CURLOPT_REFERER, $ref);//带来的Referer
}else{
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
}
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $post); // Post提交的数据包
curl_setopt($curl, CURLOPT_COOKIEJAR, $GLOBALS['cookie_file']); // 存放Cookie信息的文件名称
curl_setopt($curl, CURLOPT_COOKIEFILE,$GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');//解释gzip
curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {echo 'Errno'.curl_error($curl);}
$data[]=curl_getinfo($curl);
curl_close($curl); // 关键CURL会话
$tmpInfo=preg_replace('/script/','js',$tmpInfo);
$data[]=$tmpInfo;
return $data; // 返回数据
}
//截取字符
function preg_message($message, $rule, $getstr, $limit=1) {
$result = array('0'=>'');
$rule = conver_trule($rule); //转义正则表达式特殊字符串
$rule = str_replace('\['.$getstr.'\]', '\s*(.+?)\s*', $rule); //解析为正则表达式
if($limit == 1) {
preg_match("/$rule/is", $message, $rarr);
if(!empty($rarr[1])) {
$result[0] = $rarr[1];
}
} else {
preg_match_all("/$rule/is", $message, $rarr);
if(!empty($rarr[1])) {
$result = $rarr[1];
}
}
return $result;
}
/**
* 转义正则表达式字符串
*/
function conver_trule($rule) {
$rule = preg_quote($rule, "/"); //转义正则表达式
$rule = str_replace('\*', '.*?', $rule);
$rule = str_replace('\|', '|', $rule);
return $rule;
}
/*转编码*/
function escape($str) {
preg_match_all("/[-].|[\x01-]+/",$str,$r);
$ar = $r[0];
foreach($ar as $k=>$v) {
if(ord($v[0]) < 128)
$ar[$k] = rawurlencode($v);
else
$ar[$k] = "%u".bin2hex(iconv("GB2312","UCS-2",$v));
}
return join("",$ar);
}