简体   繁体   中英

Finding text on the page in the Chromium tab open with Puppeteer

I have a currently working project in puppeteer. Finally, I want it to search for words on the page that is open on the tab in the Chromium window that is open on the screen, but the open page, not a specific link. If there is any one to search for more than one word, if there is no "one of the words found" in ture.txt, it needs to print a sentence such as "not found any of the words" in false.txt and it will do this operation one after the other every time, how can I do it?

Hei, first of all u need an array with all ur keywords, then u can search with:

const wordsToSearch = ['firstword', 'secondword', 

'thirdword', 'fourthword'];

const contentPage = await page.$eval('body', el => el.textContent);
const result = contentPage.split(/\s+/).filter(text => wordsToSearch.includes(text));

in result you'll have all the words found.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM