Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

javascript - 如何使用带有nodejs的cheerio在已解析的HTML中查找特定的JavaScript代码?(How to find specific JavaScript code in parsed HTML using cheerio with nodejs?)

I want to find a single JavaScript code/keyword like zopim or v2.zopim.com in whole script with using cheerio on NodeJS.(我想在NodeJS上使用cheerio在整个脚本中找到一个像zopimv2.zopim.com这样的JavaScript代码/关键字。)

I wrote a script that grabs all links from a single web site but the script need to opens all these grabbed links and search for "zopim" keyword in JavaScript codes.(我编写了一个脚本,可以捕获来自单个网站的所有链接,但是该脚本需要打开所有这些捕获的链接,并在JavaScript代码中搜索“ zopim”关键字。) I looked cheerio's repository and it's issues but no luck.(我查看了cheerio的存储库 ,这是问题,但是没有运气。) I'm wondering could anyone help me to figure out this situation?(我想知道有人能帮助我解决这种情况吗?) This is a part of my code that where I open links and search in source code for a keyword.(这是我的代码的一部分,我在其中打开链接并在源代码中搜索关键字。) I can post all of it if it's necessary.(如果有必要,我可以发布所有内容。) 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(); }); } ); } }   ask by tpbafk translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use :contains selector to find scripts which contain keyword 'zopim' in text and then count found script elements:(您可以使用:contains选择器来查找文本中包含关键字'zopim'脚本,然后计算找到的脚本元素:)

const scriptsWithKeywordCount = $('script:contains("zopim")').length; if (scriptsWithKeywordCount > 0) { // webpage contains keyword in javascript code }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...