php通过关键字获取海关编码

技术 置顶 精帖
0 195
peng49
peng49 2024-01-23 18:59:00
 
  1. class HsCode {
  2. public function curlGet($url) {
  3. $curl = curl_init($url);
  4. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  5. curl_setopt($curl, CURLOPT_TIMEOUT, 10);
  6. curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36');
  7. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  8. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  9. $resp = curl_exec($curl);
  10. curl_close($curl);
  11. return $resp;
  12. }
  13. public function keywordGetHsCode($keyword = '篮球') {
  14. $page = 1;
  15. do {
  16. $content = $this->curlGet("https://www.hsbianma.com/Search/{$page}?keywords={$keyword}");
  17. preg_match_all('/<tr class="result-grid">.*?<\/tr>/s', $content, $matches);
  18. $items = array_filter(array_map(function ($tr) {
  19. if (preg_match('/"\/Code\/(\d+)\.html".*?<td>(.*?)<\/td>/s', $tr, $match)) {
  20. list($name, $nameEn) = preg_split('/<span.*?>/', $match[2]);
  21. $name = trim(preg_replace('/<\/?\w+.*?>/s', '', $name));
  22. $nameEn = trim(preg_replace('/<\/?\w+.*?>/s', '', $nameEn));
  23. return [
  24. 'hsCode' => $match[1],
  25. 'name' => $name,
  26. 'nameEn' => $nameEn
  27. ];
  28. }
  29. return [];
  30. }, $matches[0] ?: []));
  31. $hsCode = '';
  32. foreach ($items as $item) {
  33. if (strtolower($item['name']) == strtolower($keyword)) {
  34. $hsCode = $item['hsCode'];
  35. break;
  36. }
  37. }
  38. $page++;
  39. } while (empty($hsCode) && $page <= 5);
  40. return $hsCode;
  41. }
  42. }
  43. $hsCode = (new HsCode())->keywordGetHsCode('乒乓球');
  44. var_dump($hsCode);
回帖
登录
忘记密码?