二手车之家关注新车上架

目的

在深圳拍了个牌,想卖个二手车,听说二手车之家的车不错,天天打开网页看看新上架的车,又觉得浪费时间,就想写个脚本,每天定时抓取, 有新车上架,就自动发短信,或邮件 提醒,就知道有新车打开网页即可浏览。

<?php
/**
 * Created by PhpStorm.
 * User: Galaxyliu
 * Date: 2019/9/10
 * Time: 9:22
 */


require "init.php";



class auto168{

    function HttpGet($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
        curl_setopt($ch, CURLOPT_PROXY, "http://proxy.bac.com"); //代理服务器地址
        curl_setopt($ch, CURLOPT_PROXYPORT, 9090); //代理服务器端口
        curl_setopt($ch, CURLOPT_TIMEOUT,3);
        $output = curl_exec($ch);
//        $curl_info = curl_getinfo($ch);
//        print_r($curl_info);
        curl_close($ch);
        return $output;
    }


    function GetCarData($url){
        $content = $this->HttpGet($url);
        if ($content==false){

            Core_Error::add(__LINE__,"请求连接失败");

            return false;
        }

        $pattern = "/<input id=\"(.*?)\"\s+pubdate=\"(.*?)\"\s+cid=\"(.*?)\"\s+seriesid=\"(.*?)\"\s+price=\"(.*?)\"\s+";
        $pattern .= "isloan=\"(.*?)\"\s+monthpay=\"(.*?)\"\s+specid=\"(.*?)\"\s+dealerid=\"(.*?)\"\s+pic=\"(.*?)\"\s+";
        $pattern .= "mileage=\"(.*?)\"\s+regyear=\"(.*?)\"\s+seriesname=\"(.*?)\"\s+specname=\"(.*?)\"\s+gbox=\"(.*?)\"\s+";
//        $pattern .= "monthpay=";
        $pattern .= "/";
        $mathes=[];

        if (preg_match_all($pattern,$content,$mathes)==false){
            Core_Error::add(__LINE__,"正则匹配内容失败");
            return false;
        }

        $carInfo =[];
        foreach ($mathes[0] as $key=>$loop ) {
            $id = preg_replace('/[a-z]/i','',$mathes[1][$key]);
            if ($mathes[9][$key]>0){
                //商家车型链接
                $addUrl = 'https://www.che168.com/dealer/'.$mathes[9][$key].'/'.$id.'.html';
            }else{
                //个人卖家车型链接
                $addUrl = 'https://www.che168.com/personal/'.$id.'.html';
            }

            $carInfo[$mathes[1][$key]]=[
                '登记时间'=>$mathes[2][$key],
                '型号'=>$mathes[15][$key].' '.$mathes[14][$key],
                '品牌'=>$mathes[13][$key],
                '价格'=>$mathes[5][$key].'万',
                '公里数'=>$mathes[11][$key].'万公里',
                '入库'=>date('Y-m-d'),
                '状态'=>'上线',
                '地址'=>$addUrl,
                '_id'=>$mathes[1][$key],
            ];
        }
        return $carInfo;

    }


    function CarCheck($url,$newCarPrice){
        $data = $this->GetCarData($url);
        if ($data==false){
            Core_Error::add(__LINE__,"获取汽车信息失败");
            return false;
        }

        //测试数据数据
        $mongodb_test = Library_Mongo::selectDB('main','test');

        //连接表
        $collection = $mongodb_test->selectCollection("168car");

        $newIdList = array_column($data,'_id');

        $condition =['状态'=>'上线','等级'=>$newCarPrice];
        //获取正式表数据
        $data_list = $collection->find($condition);

//        print_r($data_list->toArray());die;

        $oldIdList = array_column($data_list->toArray(),'_id');

        $addIdList = array_diff($newIdList,$oldIdList);

        //下线
        $offLineIdList = array_diff($oldIdList,$newIdList);

        $addDataList =[];
        foreach ($addIdList as $newId ) {
            $row = $data[$newId];
            $row['等级']=$newCarPrice;
            $addDataList[] = $row;
        }

        //已下架的汽车
        if ($offLineIdList){
            $condition =['_id'=>['$in'=>array_values($offLineIdList)]];
            $fieldInfo =['状态'=>'下架','下架日期'=>date('Y-m-d')];
            $result =  $collection->updateMany($condition,array('$set'=>$fieldInfo));
        }

        //新上线的汽车
        if ($addDataList){
            $result = $collection->insertMany($addDataList);
//            $result = OAClass::sendMail("Galaxytest@abc.com",'Galaxyliu@abc.com','auto-car-addNew',print_r($addDataList,1));
//            var_dump($result);

        }

    }

    function run(){

        //丰田二手车 7.5-8.5万的车
        $carolaUrl ='https://m.che168.com/shenzhen/fengtian/7.5_8.5/a0_5msdg2scncgpi1ltocspexu1a0m0_6/';

        //丰田二手车 4.5-6万的车
//        $viosurl ='https://m.che168.com/shenzhen/fengtian/4.5_6/a0_5msdg2scncgpi1ltocspexu1a0m0_6/';
        $viosurl ='https://m.che168.com/shenzhen/fengtian/4.5_6/a0_5msdg2scncgpi1ltocspexu1a0/';

//        $this->CarCheck($carolaUrl,15);
//
        $this->CarCheck($viosurl,10);
  
    }




}

$auto168 = new auto168;
$auto168->run();


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值