欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 新闻 > 会展 > 一个可以在浏览器console内运行的极简爬虫,可列出网页内指定关键词的所有句子。

一个可以在浏览器console内运行的极简爬虫,可列出网页内指定关键词的所有句子。

2025/2/7 15:17:23 来源:https://blog.csdn.net/MAILLIBIN/article/details/145472378  浏览:    关键词:一个可以在浏览器console内运行的极简爬虫,可列出网页内指定关键词的所有句子。

用浏览器打开你想要分析的网页,按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);

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com