php ecshop采集商品添加规则

ecshop采集商品添加规则

<?php
header("Content-type:text/html;charset=utf-8");

function get($url)
{
global $curl;
// 配置curl中的http协议->可配置的荐可以查PHP手册中的curl_
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($curl, CURLOPT_HEADER, FALSE);
// 执行这个请求
return curl_exec($curl);
}

// 生成一个curl对象
$curl = curl_init();
$url='http://www.96net.com.cn/category.php?id='.$_GET["id"].'&page='.$_GET["page"];
$data=get($url);
//var_dump($data);
//$list_preg = '/<ul class="profl"><li>.+<\/li></ul>/Us';
//preg_match_all($list_preg,$data,$list);
//var_dump($list);

$userinfo = "Name: <b>PHP</b> <br> Title: <b>Programming Language</b>";
preg_match_all ("/<ul class=\"profl\">(.*)<\/ul>/Us", $data, $pat_array);
//print_r($pat_array[1][0]);

//$pattern="/<li><a title=\"(.*)\" target=\"_blank\" href=\"(.*)\">/iUs";//正则

preg_match_all ("/<img src=\"(.*)\" \/>/Us", $pat_array[1][0], $pat_array1); //图片路径

//var_dump($pat_array1);

preg_match_all ("/<span>市场零售价:¥(.*)元<\/span>/Us", $pat_array[1][0], $pat_array2); //价格
//var_dump($pat_array2);

preg_match_all ("/<a href=\"(.*)\" target=\"_blank\">+[\s\S]+<img src=\"(.*)\" \/>(.*)<br \/><span>(.*)<\/span>/iU", $pat_array[1][0], $pat_array3); //图片,标题,价格
//var_dump($pat_array3);

//foreach ($pat_array3[3] as $k => $v) {
//echo mb_substr($v, 7, -1, 'utf-8');
//}


foreach ($pat_array3[2] as $k => $v) {

// //获取空间头像图片
$url44 = "http://www.96net.com.cn/".$v;
$curl44 = curl_init($url44);
//$filename = date("Ymdhis").".jpg";
curl_setopt($curl44,CURLOPT_RETURNTRANSFER,1);
$imageData44 = curl_exec($curl44);
curl_close($curl44);
$aa44=dirname($v);
//echo $v;
//exit;
if(!file_exists($aa44)){
// @mkdir($aa);
mkdir($aa44,0777,true);
}
$tp44 = @fopen('./'.$v, 'a');
fwrite($tp44, $imageData44);
fclose($tp44);
//$path="./".$v;
//file_put_contents($path,$imageData);

}

 


foreach ($pat_array3[1] as $k => $v) {

// 生成一个curl对象
$curl15 = curl_init();
$url15='http://www.96net.com.cn/'.$v;
$data15=get($url15);
preg_match_all ("/<div class=\"prod_l\"><img src=\"(.*)\" style=\"widdth:400px;height:400px;\" \/><\/div>/Us", $data15, $pat_arrayyyy);

preg_match_all ("/<div class=\"prod_con_txt\">(.*)<\/div>/Us", $data15, $pat_arraxxx);

//preg_match_all ("/<p><img src=\"(.*)\" \/><\/p>/Us", $pat_arraxxx[1][0], $pat_arraxxx111);

preg_match_all ("/<p><img alt=\"\" src=\"(.*)\" \/><\/p>/Us", $pat_arraxxx[1][0], $pat_arraxxx111);

//var_dump($pat_arraxxx111);
//exit;

get1($pat_arrayyyy[1][0]);

get2($pat_arraxxx111[1]);

}

function get1($v){
// //获取空间头像图片
$url = "http://www.96net.com.cn/".$v;
$curl = curl_init($url);
//$filename = date("Ymdhis").".jpg";
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
$imageData = curl_exec($curl);
curl_close($curl);
$aa=dirname($v);
//echo $v;
//exit;
if(!file_exists($aa)){
// @mkdir($aa);
mkdir($aa,0777,true);
}
$tp = @fopen('./'.$v, 'a');
fwrite($tp, $imageData);
fclose($tp);
}

function get2($vvv){

//var_dump($vvv);
//exit;

foreach ($vvv as $k => $v) {
// //获取空间头像图片
$url = "http://www.96net.com.cn".$v;
$curl = curl_init($url);
//$filename = date("Ymdhis").".jpg";
curl_setopt($curl,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl, CURLOPT_TIMEOUT,600);
$imageData = curl_exec($curl);
curl_close($curl);
$aa=dirname($v);
//echo $v;
//exit;

$tp = @fopen('./'.$v, 'a');
fwrite($tp, $imageData);
fclose($tp);

}

}

//var_dump($weburl);


//插入数据库
$conm = mysql_connect("qdm213439548.my3w.com","qdm213439548","mms123456");
mysql_query("set names utf8");
$select_db = mysql_select_db('qdm213439548_db');

 

if (!$select_db) {
die("could not connect to the db:\n" . mysql_error());
}

 

foreach($pat_array3[2] as $y=>$val){

// 生成一个curl对象
$curl2 = curl_init();
$url2='http://www.96net.com.cn/'.$pat_array3[1][$y];
$datattt=get($url2);
preg_match_all ("/<div class=\"prod_l\"><img src=\"(.*)\" style=\"widdth:400px;height:400px;\" \/><\/div>/Us", $datattt, $pat_arrayyyy);
preg_match_all ("/<div class=\"prod_con_txt\">(.*)<\/div>/Us", $datattt, $pat_arraxxx);

$bb=mb_substr($pat_array3[4][$y], 7, -1, 'utf-8');
$tt=time();
$sid=trim($_GET["id"]);
$goodname=trim($pat_array3[3][$y]);

$goods_thumb=$pat_array3[2][$y];
$goods_img=$pat_arrayyyy[1][0];
$goods_desc=$pat_arraxxx[1][0];


$sql="INSERT ecs_goods (cat_id, goods_name, market_price, goods_thumb, goods_img,goods_desc, is_real, is_on_sale, is_alone_sale, add_time, last_update)
VALUES (114,'$goodname',$bb,'$goods_thumb','$goods_img','$goods_desc',1,1,1,'$tt','$tt')";

mysql_query($sql);
}

//查询代码

//关闭数据库连接
mysql_close($conm);

 

posted @ 2018-07-31 20:04  学无边涯  阅读(371)  评论(0编辑  收藏  举报