option('import'); if (!empty($supplier_id)) { $this->import($supplier_id); $this->line("供应商 $supplier_id 导入完毕"); } else { // $this->exists_update = false; // $this->tourist_line(); // $this->hotel(); $this->scenic(); $this->line('全部采集完毕'); } return Command::SUCCESS; } //导入 private function import($supplier_id) { $limit = $this->option('limit'); $cpModel = CollectProduct::query()->orderBy('id', 'desc'); if (!empty($limit)) { $arr = explode(',', $limit); $count = count($arr); if ($count == 1) { $import_data = $cpModel->limit($arr[0])->get()->toArray(); } else if ($count == 2) { $import_data = $cpModel->offset($arr[0])->limit($arr[1])->get()->toArray(); } else { $import_data = $cpModel->get()->toArray(); } } else { $import_data = $cpModel->get()->toArray(); } if (ctype_digit($supplier_id)) { $ids = [$supplier_id]; } else if ($supplier_id == 'all') { $ids = Supplier::query()->where('id', '>', 1)->pluck('id'); } else { return; } foreach ($ids as $supplier_id) { $this->line('正在导入 ' . $supplier_id); array_walk($import_data, function ($v) use ($supplier_id) { $v['supplier_id'] = $supplier_id; unset($v['unique_id'], $v['site']); Product::query()->updateOrCreate(['supplier_id' => $supplier_id, 'title' => $v['title']], $v); }); $this->line("导入 $supplier_id 结束"); } } //景区采集 private function scenic() { $http = Http::withOptions(['verify' => false])->withHeaders(['User-Agent' => 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1']); for ($i=15; $i<20; $i++) { $this->line('开始采集景区:第 ' . ($i + 1) . ' 页'); $html = $http->get('https://m.mafengwo.cn/jd/10030/gonglve.html?page=' . ($i + 1)); if (preg_match_all('/data-url="(.*?)"/', $html, $matches)) { if(empty($matches[1])) continue; foreach ($matches[1] as $url) { $this->line('开始采集 ' . $url); $res = $http->get('https://m.mafengwo.cn' . $url); if (!preg_match('/\/(\d+)\.html/', $url, $match)) continue; $id = $match[1]; if ($this->exists_update == false && CollectProduct::where(['unique_id' => $id, 'site' => 1])->exists()) { continue; } $inc_sales = $http->post('https://m.mafengwo.cn/poi/poi/inc_sales', ['poiid' => $id]); preg_match('#¥(\d+)#', $inc_sales['html'] ?? '', $match_price); preg_match('#
地址:(.*?)
#', $res, $match_address); $pos_start = strpos($res, '入住时间:" . (!empty($guide_info['check_in']['title']) ? $guide_info['check_in']['title'] : '') . "
"; $know .= "离店时间:" . (!empty($guide_info['check_out']['title']) ? $guide_info['check_out']['title'] : '') . "
"; $know .= '' . array_reduce($base_info['facility_sort'] ?? [], fn($v1, $v2) => $v1 . $v2['title'] ?? '') . '
'; //扩展字段 $extends['field_1_tags'] = array_map(fn($v) => $v['title'] ?? '', $guide_info['facility'] ?? []); $extends['field_1_name'] = $base_info['name']; $extends['field_1_address'] = $base_info['address']; $extends['field_1_latitude'] = $base_info['lat']; $extends['field_1_longitude'] = $base_info['lng']; CollectProduct::updateOrCreate(['unique_id' => $v['id'], 'site' => 1], [ 'unique_id' => $v['id'], 'site' => 1, 'type' => 1, //0:旅游线路、1:酒店、2:景区、3:餐厅、4:车队、5:单项 'title' => mb_substr($base_info['name'] ?? '' . $base_info['level'] ?? '', 0, 255), 'price' => mt_rand(150, 350), 'original_price' => mt_rand(350, 550), 'pictures' => array_map(fn($v) => $v['url'] ?? '', $base_info['album'] ?? []) ?? [], 'stock' => mt_rand(1000, 9999), 'sale' => $base_info['num_collect'] ?? 0, 'status' => -2, //-2下架 'know' => $know, 'content' => $guide_info['intro'] ?? '', 'extends' => $extends, 'longitude' => $base_info['lng'] ?? 0, 'latitude' => $base_info['lat'] ?? 0, 'address' => $base_info['address'] ?? '', ]); $this->line("{$v['id']} 采集结束" . PHP_EOL); } $this->line('第 ' . ($i + 1) . ' 页采集结束' . PHP_EOL); } } //酒店详情计算_sn private function _sn($params): string { ksort($params); return substr(md5(json_encode($params) . 'c9d6618dbc657b41a66eb0af952906f1'), 2, 10); } //旅游线路采集 private function tourist_line() { $http = Http::withOptions(['verify' => false]); for($i=0; $i<10; $i++) { $this->line('开始采集旅游线路:第 ' . ($i + 1) . ' 页'); $data = $http->get('https://m.mafengwo.cn/sales/ajax.php', [ 'sF' => 'search_new_list', 'offset' => $i * 10, //分页参数 ]); if (empty($data['data'])) { continue; } $data = $data['data']; preg_match_all('/exists_update == false && CollectProduct::where(['unique_id' => $id, 'site' => 1])->exists()) { continue; } $this->line('开始采集:' . $id); $info = $http->get('https://m.mafengwo.cn/sales/detail/index/info?id=' . $id); //旅游须知 $know = $info['data']['list']['content'][0]['content'] ?? []; if (isset($info['data']['list']['content'][0]['content']) && is_array($info['data']['list']['content'][0]['content'])) { $know = current(array_filter($info['data']['list']['content'][0]['content'], fn($v) => isset($v['name']) && $v['name'] == '购买须知')); if (isset($know['content']) && is_array($know['content'])) { $know = array_reduce( $know['content'], fn($v1, $v2) => $v1 . (isset($v2['name']) && is_string($v2['name']) ? "