-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (33 loc) · 1.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
const puppeteer = require("puppeteer");
const checkWordMeaning = async (word) => {
//launch the browser and open a new page
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
//navigate to the URL on the page
await page.goto(`https://www.wordnik.com/words/${word}`);
//set the screen size
await page.setViewport({ width: 1080, height: 1024 });
//take a screenshot of the screen
await page.screenshot({
path: "screenshot.png",
});
//extract the meanings from the list of meanings
const wordMeaningObject = await page.evaluate(() => {
//select all li elements that contain word meanings
const meaningElements = document.querySelectorAll(
"#define > div > ul:nth-child(4) > li"
);
//extract the meanings if available
const firstMeaning =
meaningElements[0]?.innerText.replace(/noun/g, " ").replace(/;/g, ",") ||
"Not found";
return firstMeaning;
});
//print the extracted meanings
console.log(wordMeaningObject);
//close the browser
await browser.close();
};
//declare a variable to search for and pass it
const wordtosearch = "software";
checkWordMeaning(wordtosearch);