后台代码
function caiji(){
header("Content-type: text/html; charset=utf-8");
$url=$_POST['taobao_url'];
//$url="https://item.taobao.com/item.htm?spm=a21bo.2017.201867-rmds-15.3.5af911d9ZEcbiK&scm=1007.12807.84406.100200300000004&id=540021976353&pvid=07ca0d9b-36f5-4484-b816-8989542f8beb";
$text=file_get_contents($url);
$data=array();
//http://www.22.com/index.php?c=shop_market&act=caiji&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]);
// var_dump($title1);
$data['name']=$title1;
//3商品价格
preg_match('/<input[^>]*name="current_price" [^>]*>/', $text, $price);
$price1=explode('"',$price[0]);
//$price=floatval($price);//放入数据库估计还有转一下变量类型
$data['cost']=$price1[5];
//var_dump($price1[5]);
//4商品描述
preg_match_all('/<script[^>]*>[^<]*<\/script>/is', $text, $content);//页面js脚本
$content=$content[0];
$description='<div id="detail"> </div>
<div id="description">
<div id="J_DivItemDesc">描述加载中</div>
</div>';
foreach ($content as &$v){
$description.=iconv('GBK','UTF-8',$v);
};
//var_dump($description);
$miaoshu= explode(':',$description);
$xiangqing = explode('//',$miaoshu[14]);//商品详情地址
$xiangqing_url ="http://".$xiangqing[1];
$xiangqing1 =file_get_contents($xiangqing_url);
$xiangqing2=iconv('GBK','UTF-8',$xiangqing1);
$xiangqing3 = explode('desc=',$xiangqing2);
$data['xiangqing']= stripslashes($xiangqing3[1]);
//var_dump($xiangqing3[1]);
$this->actText->result('taobao_xq',$data);
//var_dump($data);
//return
$this->display();
}
前台代码
<script type="text/javascript">
function caiji(){
var taobao_url = $("input[name='taobao_url']").val();
$.ajax({
type: 'post',
data:{'taobao_url':taobao_url},
url: '<{$siteurl}>/index.php?c=shop_market&act=caiji&datatype=json&random=@random@'.replace('@random@', 1+Math.round(Math.random()*1000)),
dataType: 'json',
success: function(content) {
console.log(content.msg.taobao_xq);
if(content.msg.taobao_xq.name==""){
diaerror('未获取到数据');
}else{
$("input[name='i_name']").val(content.msg.taobao_xq.name);
$("input[name='i_cost']").val(content.msg.taobao_xq.cost);
$("#i_instro").html(content.msg.taobao_xq.xiangqing);
}
},
error: function(content) {
diaerror('数据获取失败');
}
});
}
</script>
共同学习,写下你的评论
评论加载中...
作者其他优质文章