用浏览器打开你想要分析的网页,按F12,出现console栏后,在console内粘贴以下的代码,即可显示网页内指定关键词的所有句子。(代码中的关键词可以自己修改)
//此程序可以列出网页中包含指定关键词的所有句子 console中粘贴即可运行
function findSentencesWithKeyword(keyword) {
// 获取页面所有文本节点
let textNodes = [];
function getTextNodes(node) {
if (node.nodeType === Node.TEXT_NODE && node.textContent.trim()) {
textNodes.push(node);
} else {
for (let child of node.childNodes) {
getTextNodes(child);
}
}
}
getTextNodes(document.body);
// 查找包含关键词的句子
let sentences = [];
for (let node of textNodes) {
let text = node.textContent;
// 使用简单句号分割方法,但这可能不完全准确
let potentialSentences = text.split('. ');
for (let sentence of potentialSentences) {
if (sentence.toLowerCase().includes(keyword.toLowerCase())) {
sentences.push(sentence.trim() + '.');
}
}
}
// 在控制台打印结果
console.log('包含关键词 "' + keyword + '" 的句子:');
sentences.forEach((sentence, index) => {
console.log(`${index + 1}. ${sentence}`);
});
}
// 这里设置你的关键词
let keyword = '边牧'; // 替换为你需要的关键词
findSentencesWithKeyword(keyword);