如何使用cheerio与向的NodeJS找到特定的JavaScript代码的HTML剪掉指甲?
我想找到一个JavaScript代码/像zopim
或v2.zopim
在整个脚本的关键字使用上的NodeJS cheerio。我写了,抓住从一个单一的网站,但脚本中的所有环节需要打开所有这些抓起链接和搜索在JavaScript代码“zopim”关键字的脚本。我看着cheerio的repository和它的issues但没有运气。我想知道,任何人都可以帮助我弄清楚这种情况呢?这是我的代码如下部分,我打开链接,在源代码中搜索关键字。我可以张贴的,如果它是必要的。
function () {
//console.log(totalUrls);
console.log("Crawling is done.")
if (page == 16) {
console.log("Anaylzing web sites...");
async.whilst(
function () {
return checkedUrl < totalUrls.length;
},
function (urlCallback) {
var currentUrl = totalUrls[checkedUrl]
request(currentUrl, function (err, res, body) {
if (err) {
console.log('Error: ' + err);
}
var $ = cheerio.load(body);
$('.headerContent').each(function () {
var title = $(this).find('a').text();
console.log(currentUrl + title);// if the current web site has a '.headerContent' class print it.
// I want to print only if web site source code includes "zopim" keyword in JavaScript code
});
checkedUrl++;
urlCallback();
});
}
);
}
}
回答如下:
您可以使用:contains
选择找到包含在文本关键字'zopim'
脚本,然后计算发现脚本元素:
const scriptsWithKeywordCount = $('script:contains("zopim")').length;
if (scriptsWithKeywordCount > 0) {
// webpage contains keyword in javascript code
}