aizhan.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. require_once('vendor/autoload.php');
  3. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  4. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  5. $word = $_REQUEST['t'];
  6. if (!$word){
  7. ?>
  8. <html lang="zh-cn">
  9. <head>
  10. <meta charset="utf-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1">
  13. <!-- ZUI 标准版压缩后的 CSS 文件 -->
  14. <link rel="stylesheet" href="//cdn.bootcss.com/zui/1.8.1/css/zui.min.css">
  15. <!-- ZUI Javascript 依赖 jQuery -->
  16. <script src="//cdn.bootcss.com/zui/1.8.1/lib/jquery/jquery.js"></script>
  17. <!-- ZUI 标准版压缩后的 JavaScript 文件 -->
  18. <script src="//cdn.bootcss.com/zui/1.8.1/js/zui.min.js"></script>
  19. </head>
  20. <body>
  21. <br>
  22. <br>
  23. <br>
  24. <br>
  25. <form method="post" action="">
  26. <div class="container">
  27. <div class="row">
  28. <div class="col-md-9">
  29. <input type="text" class="form-control" placeholder="请输入关键词" name="t"/>
  30. </div>
  31. </div>
  32. <div class="row" style="margin-top: 5px;">
  33. <div class="col-md-2 col-md-offset-8">
  34. <input type="submit" class="btn btn-primary btn-lg" value="提交"/>
  35. </div>
  36. </div>
  37. </div>
  38. </form>
  39. </body>
  40. </html>
  41. <?php
  42. die();
  43. }
  44. $js = new JS();
  45. $header = [[
  46. '关键词',
  47. 'PC',
  48. '移动'
  49. ]];
  50. $html = $js->getPage($word);
  51. $ql = (new QL\QueryList)->html($html);
  52. $table = $ql->find('table');
  53. // 采集表头
  54. $tableHeader = $table->find('tr:eq(0)')->find('td')->texts();
  55. // 采集表的每行内容
  56. $tableRows = $table->find('tr:gt(0)')->map(function ($row) {
  57. return $row->find('td')->texts()->all();
  58. });
  59. //print_r($tableHeader->all());
  60. $data = $tableRows->all();
  61. $rt = $ql->find('.pager li');
  62. $pages = $rt->count();
  63. for ($page = 2; $page <= $pages; $page++) {
  64. $_data = $js->getPage($word, $page);
  65. $_ql = (new QL\QueryList)->html($html);
  66. $table = $_ql->find('table');
  67. // 采集表的每行内容
  68. $tableRows = $table->find('tr:gt(0)')->map(function ($row) {
  69. return $row->find('td')->texts()->all();
  70. });
  71. $_data = $tableRows->all();
  72. if ($_data) {
  73. $data = array_merge($data, $_data);
  74. }
  75. }
  76. $new_data = array_map(function ($a) {
  77. $pat = "#\d+#";
  78. preg_match_all($pat, $a[2], $arr);
  79. return [
  80. 'title' => $a[1],
  81. 'pc' => $arr[0][0],
  82. 'mobile' => $arr[0][1],
  83. ];
  84. }, $data);
  85. $data=array_merge($header,$new_data);
  86. $spreadsheet = new Spreadsheet();
  87. try {
  88. $title="关键词挖掘-" . $word;
  89. $sheet = $spreadsheet->getActiveSheet();
  90. $sheet->setTitle($title);
  91. $sheet->fromArray($data);
  92. $writer = new Xlsx($spreadsheet);
  93. header("Pragma: public");
  94. header("Expires: 0");
  95. header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
  96. header("Content-Type:application/force-download");
  97. header("Content-Type:application/vnd.ms-execl");
  98. header("Content-Type:application/octet-stream");
  99. header("Content-Type:application/download");
  100. header('Content-Disposition:attachment;filename="'.$title.'.xlsx"');
  101. header("Content-Transfer-Encoding:binary");
  102. $writer->save('php://output');
  103. $spreadsheet->disconnectWorksheets();
  104. } catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
  105. }
  106. class JS
  107. {
  108. private $cookie_file = "/www/cookie_wyt.txt";
  109. public function getPage($word, $page = 1)
  110. {
  111. $word = $this->encode($word);
  112. $curl = curl_init();
  113. if ($page == 1) {
  114. $url = "https://ci.aizhan.com/" . $word . "/";
  115. } else {
  116. $url = "https://ci.aizhan.com/" . $word . "/" . $page;
  117. }
  118. curl_setopt_array($curl, array(
  119. CURLOPT_URL => $url,
  120. CURLOPT_RETURNTRANSFER => true,
  121. CURLOPT_ENCODING => "",
  122. CURLOPT_MAXREDIRS => 10,
  123. CURLOPT_TIMEOUT => 0,
  124. CURLOPT_FOLLOWLOCATION => true,
  125. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  126. CURLOPT_CUSTOMREQUEST => "GET",
  127. CURLOPT_HTTPHEADER => array(
  128. "Connection: keep-alive",
  129. "Pragma: no-cache",
  130. "Cache-Control: no-cache",
  131. "Upgrade-Insecure-Requests: 1",
  132. "DNT: 1",
  133. "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",
  134. "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",
  135. "Sec-Fetch-Site: same-origin",
  136. "Sec-Fetch-Mode: navigate",
  137. "Sec-Fetch-User: ?1",
  138. "Sec-Fetch-Dest: document",
  139. "Referer: https://ci.aizhan.com/",
  140. "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8",
  141. "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"
  142. ),
  143. ));
  144. curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookie_file); //连接结束后保存cookie信息的文件。
  145. curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookie_file); //包含cookie数据的文件名,cookie文件的格式可以是Netscape格式,或者只是纯HTTP头部信息存入文件。
  146. $response = curl_exec($curl);
  147. curl_close($curl);
  148. return $response;
  149. }
  150. /**
  151. * 将字符串转换为ascii码
  152. * @param string $c 要编码的字符串
  153. * @param string $prefix 前缀,默认:&#
  154. * @return string
  155. */
  156. public function encode($c, $prefix = "&#")
  157. {
  158. $len = strlen($c);
  159. $a = 0;
  160. $scill = '';
  161. while ($a < $len) {
  162. $ud = 0;
  163. if (ord($c{$a}) >= 0 && ord($c{$a}) <= 127) {
  164. $ud = ord($c{$a});
  165. $a += 1;
  166. } else if (ord($c{$a}) >= 192 && ord($c{$a}) <= 223) {
  167. $ud = (ord($c{$a}) - 192) * 64 + (ord($c{$a + 1}) - 128);
  168. $a += 2;
  169. } else if (ord($c{$a}) >= 224 && ord($c{$a}) <= 239) {
  170. $ud = (ord($c{$a}) - 224) * 4096 + (ord($c{$a + 1}) - 128) * 64 + (ord($c{$a + 2}) - 128);
  171. $a += 3;
  172. } else if (ord($c{$a}) >= 240 && ord($c{$a}) <= 247) {
  173. $ud = (ord($c{$a}) - 240) * 262144 + (ord($c{$a + 1}) - 128) * 4096 + (ord($c{$a + 2}) - 128) * 64 + (ord($c{$a + 3}) - 128);
  174. $a += 4;
  175. } else if (ord($c{$a}) >= 248 && ord($c{$a}) <= 251) {
  176. $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);
  177. $a += 5;
  178. } else if (ord($c{$a}) >= 252 && ord($c{$a}) <= 253) {
  179. $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);
  180. $a += 6;
  181. } else if (ord($c{$a}) >= 254 && ord($c{$a}) <= 255) { //error
  182. $ud = false;
  183. }
  184. $scill .= dechex($ud);
  185. }
  186. return $scill;
  187. }
  188. }