精易论坛

标题: [我的爬虫日记06]用PHP爬取香烟价格信息 [打印本页]

作者: 易飞鸟    时间: 2024-11-9 13:14
标题: [我的爬虫日记06]用PHP爬取香烟价格信息
[PHP] 纯文本查看 复制代码
<?php
require_once "./lib/func.php";
require_once "./lib/PHPExcel.php";


function main(){

    $price = "1,100"; //价格区间

    $url = "https://www.yanyue.cn/api/rc/product/yanlist?price={$price}&page=1&pagenum=500";
   
    $headers = [
        "user-agent:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
        "host:www.yanyue.cn"
    ];

    log_print("开始获取香烟价格...",true);
   
    $ret_str = get_data($url, $headers);
    $json_data = json_decode($ret_str, true);
   
    $smoke = [
        "productname" => "",
        "typename" => "",
        "tar" => "",
        "packprice" => "",
        "barprice" => ""
    ];
   
    $smoke_list = [];

    if (is_array($json_data["productlist"] )) {
        foreach ($json_data["productlist"] as $key => $val) {
            $smoke = [
                "productname" => $val["productname"],
                "typename" => $val["type"]["typename"],
                "tar" => $val["genpicdata"]["tar"]["orgstr"],
                "packprice" => $val["genpicdata"]["packprice"]["orgstr"],
                "barprice" => $val["genpicdata"]["barprice"]["orgstr"]
            ];
            // var_print($smoke);
            array_push($smoke_list, $smoke);
        }
   
        smoke_to_excel($smoke_list,"香烟价格表.xls");
    } else {
        log_print("获取失败,请检查原因!",true,2);
    }
}



function smoke_to_excel($smoke_list, $file_name)
{
    // 创建操作对象
    $objPHPExcel = new PHPExcel();
    $objSheet = $objPHPExcel->getActiveSheet();

    // 设置表头
    $objSheet->setCellValue('A1', '香烟名称');
    $objSheet->setCellValue('B1', '类型');
    $objSheet->setCellValue('C1', '焦油');
    $objSheet->setCellValue('D1', '单盒价格');
    $objSheet->setCellValue('E1', '整条价格');

    // 设置内容
    for ($i = 0; $i < count($smoke_list); $i++) {
        $w = $i + 2;
        $smoke = $smoke_list[$i];
        $objSheet->setCellValue("A{$w}", $smoke["productname"]);
        $objSheet->setCellValue("B{$w}", $smoke["typename"]);
        $objSheet->setCellValue("C{$w}", $smoke["tar"]);
        $objSheet->setCellValue("D{$w}", $smoke["packprice"]);
        $objSheet->setCellValue("E{$w}", $smoke["barprice"]);
        log_print($smoke["productname"]."->信息写出成功",true);
    }

    // 保存
    $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
    @$objWriter->save($file_name);
    log_print("任务完成",true);
}


if (php_sapi_name() === 'cli') {
    main();
} else {
    log_print("请在命令行模型下运行!",true,2);
}




香烟价格.rar (911.95 KB, 下载次数: 21)


捕获.PNG (86.58 KB, 下载次数: 2)

捕获.PNG

作者: 神一样的人猫腻    时间: 2024-11-9 14:25
感谢分享
作者: qwe111qwe    时间: 2024-11-9 14:55
感谢分享!!!!!
作者: chuanqibuding    时间: 2024-11-9 15:47
https://www.yanyue.cn/api/rc/product/yanlist?brandid=1&typeid=0&tar=&nicotine&co=&price=&page=1&pagenum=12&mprice=&productname=%E8%8A%99%E8%93%89%E7%8E%8B 带走。感谢。
作者: airaz    时间: 2024-11-9 16:27
大哥有没有通用的。。。。哈哈哈哈
作者: 我爱阿妹i    时间: 2024-11-9 17:58
感谢分享

作者: xiaojiancccc    时间: 2024-11-9 18:21
谢谢分享
作者: 396384183    时间: 2024-11-10 00:14

楼主辛苦了,谢谢楼主,感谢楼主分享,楼主好人一生平安!!!
作者: year1970    时间: 2024-11-10 08:32
感谢分享
作者: 一指温柔    时间: 2024-11-10 09:12
支持开源~!感谢分享
作者: please    时间: 2024-11-11 09:39
感谢分享,支持开源!!!
作者: 熊不熊    时间: 2024-12-4 03:25
感谢分享,很给力!~




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4