[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, 下载次数: 13)
|