Node.js 页面主题聚焦度检测:title 和正文各说各的,AI 也读不懂
搜索引擎和 AI 模型理解一个页面,第一步是判断"这页到底在讲什么"。判断的依据,是 title、H1、正文这三处是否指向同一个主题。如果 title 说 A、正文却在讲 B,机器就会困惑,排名和 AI 摘要都跟着跑偏。这篇写一个 Node.js 工具,抓取页面,把 title、H1、正文的关键词都提出来对比,一眼看出页面主题是不是聚焦。
内容与搜索匹配的底层逻辑,谷歌官方说明看这里:Google 搜索如何工作。
工具能检查什么
- 提取页面 title、H1 的文本
- 分别统计 title、H1、正文的高频关键词
- 对比三处的关键词是否一致,判断主题是否聚焦
用 Node.js 原生 fetch,不装任何依赖。
完整代码
javascript
// topic-focus.js
// 用途:检测页面 title / H1 / 正文的关键词是否聚焦同一主题
// 运行:node topic-focus.js https://example.com/page
// 依赖:无(Node 18+ 自带 fetch)
const URL = process.argv[2] || 'https://example.com/page';
function stripHtml(s) {
return s.replace(/<[^>]+>/g, ' ').replace(/ /g, ' ').replace(/\s+/g, ' ').trim();
}
function topWords(text, n = 15) {
const words = (text.toLowerCase().match(/[a-z][a-z0-9-]{1,}|[一-龥]{2,}/g) || []);
const stop = new Set(['the','and','for','with','that','this','from','your','have','are','was','not','our','you','all','can','how','what','why','when','which','into','about','page','more','than','their','them','they','will','its','get','use','one','new','our']);
const counts = new Map();
words.forEach((w) => {
if (stop.has(w) || /^\d+$/.test(w)) return;
counts.set(w, (counts.get(w) || 0) + 1);
});
return [...counts.entries()].sort((a, b) => b[1] - a[1]).slice(0, n);
}
async function main() {
const html = await (await fetch(URL, { headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/120.0' } })).text();
const title = stripHtml((html.match(/<title[^>]*>([\s\S]*?)<\/title>/i) || [, ''])[1]);
const h1 = stripHtml((html.match(/<h1[^>]*>([\s\S]*?)<\/h1>/i) || [, ''])[1]);
const body = stripHtml(html
.replace(/<script[\s\S]*?<\/script>/gi, '')
.replace(/<style[\s\S]*?<\/style>/gi, ''));
console.log('===== 页面主题聚焦度检测 =====');
console.log('title:', title || '(缺失)');
console.log('H1:', h1 || '(缺失)');
console.log('\ntitle 关键词:', topWords(title, 5).map(([w]) => w).join(', '));
console.log('H1 关键词:', topWords(h1, 5).map(([w]) => w).join(', '));
console.log('\n正文高频词 Top 15:');
topWords(body, 15).forEach(([w, n]) => console.log(` ${w} ${n} 次`));
console.log('\n提示:title、H1 的关键词应出现在正文高频词里,对不上说明主题可能跑偏');
console.log('===== 结束 =====');
}
main();
怎么用
bash
node topic-focus.js https://你的域名/页面
运行后先看 title 和 H1 的关键词,再对照正文高频词。两边对得上,主题就是聚焦的;对不上,就要找原因。
三个常见的主题跑偏原因
title 为了博点击,和正文脱节。 title 写得很抓眼球,但正文根本没讲 title 里那件事。机器按 title 理解了页面,抓正文时发现货不对板,排名和摘要都受损。title 要准确概括正文,而不是只求点击率。
H1 和 title 各说各话。 有的页面 title 写"产品 A",H1 却写"产品 B",正文又讲别的。三个标签各说各的,机器彻底搞不清这页的主题。title 和 H1 应该指向同一件事,表述可以不同,主题必须一致。
正文堆了一堆不相关的词。 为了蹭流量,在一篇讲"保温杯"的内容里硬塞"运动水壶""咖啡杯""玻璃杯"一堆词,把正文高频词搅乱。机器判断不出这页的核心主题,反而一个都排不好。
写在最后
主题聚焦是 SEO 和 GEO 共同的地基。机器读懂"这页讲什么",后面的排名、摘要、AI 引用才有意义。title、H1、正文三处关键词对一遍,页面主题有没有跑偏,一目了然。
延伸阅读:谷歌 SEO 系统实操。
觉得有用欢迎点赞、收藏、转发,有疑问评论区留言或私信交流。