find('h3>a')->texts(); //获取搜索结果标题列表 //$links = $ql->find('h3>a')->attrs('href'); //获取搜索结果链接列表 //print_r($titles); //print_r($links); //die(); $times = 3; $wd = $_REQUEST['wd']; if (!$wd) { ?>





爬取深度
getActiveSheet(); $sheet->setTitle("为你推荐-相关搜索词"); $row = 1; $sheet->setCellValue("A" . $row, "序号"); $sheet->setCellValue("B" . $row, "关键词"); $sheet->setCellValue("C" . $row, "相关搜索词"); $row++; foreach ($result as $key => $item) { $sheet->setCellValue("A" . $row, $row - 1); $sheet->setCellValue("B" . $row, $item['key']); $sheet->setCellValue("C" . $row, $item['relatedWd']); $row++; } $styleArray = [ 'alignment' => [ 'horizontal' => Alignment::HORIZONTAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER, ], ]; $sheet->getColumnDimension("A")->setWidth(14); $sheet->getColumnDimension("B")->setWidth(25); $sheet->getColumnDimension("C")->setWidth(25); $sheet->getStyle("A1:C" . $row)->applyFromArray($styleArray); $writer = new Xlsx($spreadsheet); header("Pragma: public"); header("Expires: 0"); header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); header("Content-Type:application/force-download"); header("Content-Type:application/vnd.ms-execl"); header("Content-Type:application/octet-stream"); header("Content-Type:application/download"); header('Content-Disposition:attachment;filename="为你推荐-相关搜索词.xlsx"'); header("Content-Transfer-Encoding:binary"); $writer->save('php://output'); $spreadsheet->disconnectWorksheets(); // echo "完成,总耗时".$second."秒"; } function baidu($wds) { global $deep; for ($i = 0; $i < $deep; $i++) { if (!isset($_wds)) { $_wds = $wds; } $wds2 = []; foreach ($_wds as $wd) { $wds2 = array_merge($wds2, getRelatedKeywords($wd)); } $_wds = $wds2; } } function getRelatedKeywords($wd) { global $response, $result; $searchUrl = "http://www.baidu.com/s?ie=UTF-8&wd=%s"; $url = sprintf($searchUrl, $wd); $rules = [ 'json' => ['.c-tools', 'data-tools'], ]; $ql = (new QL\QueryList)->get($url); $rt = $ql->rules($rules)->query()->getData(); $rt = json_decode(json_encode($rt), true); $wds = []; foreach ($rt as $item) { $json = json_decode($item['json'], true); $baiduUrl = substr($json['url'], 30); if ($baiduUrl) { $_url = "https://sp2.baidu.com/8LUYsjW91Qh3otqbppnN2DJv/link?url=%s&query=%s&cb=jQuery110206243498231922682_1558178381142&data_name=recommend_common_merger_online&format=json&t=%s"; $_url = sprintf($_url, $baiduUrl, $wd, time() . "000"); $data = @file_get_contents($_url); $pat = "#1558178381142\((.*)\)#"; preg_match($pat, $data, $arr); $data = json_decode($arr[1], true); if ($data['data'][0]['hintData']) { foreach ($data['data'][0]['hintData'] as $hintDatum) { if ($response[md5($item['wd'])]) { continue; } foreach ($hintDatum['linkInfo'] as $item) { $response[md5($item['wd'])] = $item['wd']; $result[] = ['key' => $wd, 'relatedWd' => $item['wd']]; $wds[] = $item['wd']; } foreach ($hintDatum['linkInfo2'] as $item) { $response[md5($item['wd'])] = $item['wd']; $result[] = ['key' => $wd, 'relatedWd' => $item['wd']]; $wds[] = $item['wd']; } } } // print_r($data);die(); } } return $wds; }