diff --git a/app/common/utils/crawl/service/UsedCarCrawlSingle.php b/app/common/utils/crawl/service/UsedCarCrawlSingle.php index 59b88da..2f3e55e 100644 --- a/app/common/utils/crawl/service/UsedCarCrawlSingle.php +++ b/app/common/utils/crawl/service/UsedCarCrawlSingle.php @@ -227,6 +227,9 @@ class UsedCarCrawlSingle throw new \Exception('无法获取页面 HTML'); } + // che168 页面为 gb2312,Linux 默认 UTF-8,直接解析会导致 DOM 解析异常。统一转 UTF-8 再解析。 + $html = $this->ensureUtf8ForChe168($html); + // 使用 QueryList 解析 HTML $ql = QueryList::html($html); @@ -659,6 +662,32 @@ class UsedCarCrawlSingle return null; } + /** + * 将 che168 页面 (gb2312/gbk) 转为 UTF-8,避免 Linux 下 DOM 解析异常 + * + * @param string $html + * @return string + */ + protected function ensureUtf8ForChe168(string $html): string + { + if (!function_exists('mb_convert_encoding')) { + return $html; + } + $isGb = stripos($html, 'charset=gb2312') !== false || stripos($html, 'charset=gbk') !== false; + if (!$isGb) { + return $html; + } + $out = @mb_convert_encoding($html, 'UTF-8', 'GB2312'); + if ($out === false || $out === '') { + $out = @mb_convert_encoding($html, 'UTF-8', 'GBK'); + } + if ($out !== false && $out !== '') { + $html = $out; + $html = preg_replace('/charset\s*=\s*["\']?gb(2312|k)["\']?/i', 'charset="utf-8"', $html); + } + return $html; + } + /** * 安全解 JSON *