getPage($word); $ql = (new QL\QueryList)->html($html); $table = $ql->find('table'); // 采集表头 $tableHeader = $table->find('tr:eq(0)')->find('td')->texts(); // 采集表的每行内容 $tableRows = $table->find('tr:gt(0)')->map(function ($row) { return $row->find('td')->texts()->all(); }); //print_r($tableHeader->all()); $data = $tableRows->all(); $rt = $ql->find('.pager li'); $pages = $rt->count(); for ($page = 2; $page <= $pages; $page++) { $_data = $js->getPage($word, $page); $_ql = (new QL\QueryList)->html($html); $table = $_ql->find('table'); // 采集表的每行内容 $tableRows = $table->find('tr:gt(0)')->map(function ($row) { return $row->find('td')->texts()->all(); }); $_data = $tableRows->all(); if ($_data) { $data = array_merge($data, $_data); } } $new_data = array_map(function ($a) { $pat = "#\d+#"; preg_match_all($pat, $a[2], $arr); return [ 'title' => $a[1], 'pc' => $arr[0][0], 'mobile' => $arr[0][1], ]; }, $data); $data=array_merge($header,$new_data); $spreadsheet = new Spreadsheet(); try { $title="关键词挖掘-" . $word; $sheet = $spreadsheet->getActiveSheet(); $sheet->setTitle($title); $sheet->fromArray($data); $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="'.$title.'.xlsx"'); header("Content-Transfer-Encoding:binary"); $writer->save('php://output'); $spreadsheet->disconnectWorksheets(); } catch (\PhpOffice\PhpSpreadsheet\Exception $e) { } class JS { private $cookie_file = "/www/cookie_wyt.txt"; public function getPage($word, $page = 1) { $word = $this->encode($word); $curl = curl_init(); if ($page == 1) { $url = "https://ci.aizhan.com/" . $word . "/"; } else { $url = "https://ci.aizhan.com/" . $word . "/" . $page; } curl_setopt_array($curl, array( CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", CURLOPT_HTTPHEADER => array( "Connection: keep-alive", "Pragma: no-cache", "Cache-Control: no-cache", "Upgrade-Insecure-Requests: 1", "DNT: 1", "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36", "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Sec-Fetch-Site: same-origin", "Sec-Fetch-Mode: navigate", "Sec-Fetch-User: ?1", "Sec-Fetch-Dest: document", "Referer: https://ci.aizhan.com/", "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8", "Cookie: _csrf=07a32f1f032d03f1d744d53696ae7add08ec7d0fe08202093af6bd0183354165a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22meXqZAnQyb4P9Tp2mYSl8zuGXy-6uMbT%22%3B%7D; Hm_lvt_b37205f3f69d03924c5447d020c09192=1603156102; allWords=%E6%B8%B8%E6%88%8F%E4%B8%8B%E8%BD%BD%7C%E6%B5%8B%E8%AF%95%2C0; userId=19553; userName=readulikebook%40gmail.com; userGroup=1; userSecure=KsG0JKVlwgoH%2FrokCFL8oKkXf3YhJSs5DuMHwIGUJ%2FTJBhOUP18zEkv9o60j6UW8s4Nz4IALGxr%2FDrMStuTq5dKPifo%3D; Hm_lpvt_b37205f3f69d03924c5447d020c09192=1603458033" ), )); curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie_file); //连接结束后保存cookie信息的文件。 curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file); //包含cookie数据的文件名,cookie文件的格式可以是Netscape格式,或者只是纯HTTP头部信息存入文件。 $response = curl_exec($curl); curl_close($curl); return $response; } /** * 将字符串转换为ascii码 * @param string $c 要编码的字符串 * @param string $prefix 前缀,默认:&# * @return string */ public function encode($c, $prefix = "&#") { $len = strlen($c); $a = 0; $scill = ''; while ($a < $len) { $ud = 0; if (ord($c{$a}) >= 0 && ord($c{$a}) <= 127) { $ud = ord($c{$a}); $a += 1; } else if (ord($c{$a}) >= 192 && ord($c{$a}) <= 223) { $ud = (ord($c{$a}) - 192) * 64 + (ord($c{$a + 1}) - 128); $a += 2; } else if (ord($c{$a}) >= 224 && ord($c{$a}) <= 239) { $ud = (ord($c{$a}) - 224) * 4096 + (ord($c{$a + 1}) - 128) * 64 + (ord($c{$a + 2}) - 128); $a += 3; } else if (ord($c{$a}) >= 240 && ord($c{$a}) <= 247) { $ud = (ord($c{$a}) - 240) * 262144 + (ord($c{$a + 1}) - 128) * 4096 + (ord($c{$a + 2}) - 128) * 64 + (ord($c{$a + 3}) - 128); $a += 4; } else if (ord($c{$a}) >= 248 && ord($c{$a}) <= 251) { $ud = (ord($c{$a}) - 248) * 16777216 + (ord($c{$a + 1}) - 128) * 262144 + (ord($c{$a + 2}) - 128) * 4096 + (ord($c{$a + 3}) - 128) * 64 + (ord($c{$a + 4}) - 128); $a += 5; } else if (ord($c{$a}) >= 252 && ord($c{$a}) <= 253) { $ud = (ord($c{$a}) - 252) * 1073741824 + (ord($c{$a + 1}) - 128) * 16777216 + (ord($c{$a + 2}) - 128) * 262144 + (ord($c{$a + 3}) - 128) * 4096 + (ord($c{$a + 4}) - 128) * 64 + (ord($c{$a + 5}) - 128); $a += 6; } else if (ord($c{$a}) >= 254 && ord($c{$a}) <= 255) { //error $ud = false; } $scill .= dechex($ud); } return $scill; } }