I get a UnhandledPromiseRejectionWarning: Unhandled promise rejection while doing simple web scraping with puppeteer, I have used the exact same code in another projects and it worked, I dont know why it aint working now. Full eror next line:
node .\scrapers.js
(node:9748) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'getProperty' of undefined
at scrapeChannel (C:\Users\João Teixeira\OneDrive\code\learning\js\webscrapingapp\server\scrapers.js:10:27)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:9748) UnhandledPromiseRejectionWarning: Unhandled promise rejection
. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag --unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9748) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated.
In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Here's the code:
const puppeteer = require('puppeteer')
async function scrapeChannel(url) {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
const [el] = await page.$x('//*[@id="text"]');
const text = await el.getProperty('textContent');
const name = await text.jsonValue();
const [el2] = await page.$x('//*[@id="img"]');
const src = await el2.getProperty('src');
const avatarURL = await src.jsonValue();
console.log({name, avatarURL})
browser.close();
return {name, avatarURL}
}
scrapeChannel('https://www.youtube.com/user/Microsoft');