//天猫规则
function caiji1(){
header("Content-type: text/html; charset=utf-8");
$url=$_POST['tianmao_url'];//商品详情页的网络连接
//$url="https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.11.45a71005sFv2xe&id=571479997739&skuId=3703504809818&user_id=196993935&cat_id=53412001&is_b=1&rn=c39a54dfa4803508cb2ebc69a66ecfe8";
$text=file_get_contents($url);
$data=array();
//http://www.22.com/index.php?c=shop_market&act=caiji1&datatype=json
//1运用正则抓取img标签中id为J_ImgBooth的img,$img[0]为该500图img标签,$img[1]为500图的图片地址;
preg_match('/<img[^>]*id="J_ImgBooth"[^r]*rc=\"([^"]*)\"[^>]*>/', $text, $img);
$data['img']= "http:".$img[1];
//var_dump($img[1]);
//2抓取商品名称
preg_match('/<title>([^<>]*)<\/title>/', $text, $title);
$title1=iconv('GBK','UTF-8',$title[1]);
$data['name']=$title1;
//3商品价格
preg_match('/.+"price":"(.+)",.+/U', $text, $price);
$price1=$price[1];
$data['cost']=$price1;
//var_dump($price);
//4商品描述
preg_match_all('/.+"descUrl":"(.+)"/U', $text, $content);//页面js脚本
//var_dump($content);
$xiangqing = $content[1][0];//商品详情地址
$xiangqing_url ="http:".$xiangqing;
$xiangqing1 =file_get_contents($xiangqing_url);
$xiangqing2=iconv('GBK','UTF-8',$xiangqing1);
$xiangqing3 = explode('desc=',$xiangqing2);
$data['xiangqing']= stripslashes($xiangqing3[1]);
$this->actText->result('tianmao_xq',$data);
$this->display();
}
//前台js
<script type="text/javascript">
//天猫
function caiji1(){
var tianmao_url = $("input[name='tianmao_url']").val();
$.ajax({
type: 'post',
data:{'tianmao_url':tianmao_url},
url: '<{$siteurl}>/index.php?c=shop_market&act=caiji1&datatype=json&random=@random@'.replace('@random@', 1+Math.round(Math.random()*1000)),
dataType: 'json',
success: function(content) {
console.log(content.msg.tianmao_xq);
if(content.msg.tianmao_xq.name==""){
diaerror('未获取到数据');
}else{
$("input[name='i_name']").val(content.msg.tianmao_xq.name);
$("input[name='i_cost']").val(content.msg.tianmao_xq.cost);
$(".taoxq").html(content.msg.tianmao_xq.xiangqing);//带样式
$("textarea[name='i_instro']").html(content.msg.tianmao_xq.xiangqing);//纯文本
$("#taobao_log").attr('src',content.msg.tianmao_xq.img);
$("#taobao_img").val(content.msg.tianmao_xq.img);
}
},
error: function(content) {
diaerror('数据获取失败');
}
});
}