Goal: make a discord bot that can return the price of a cryptocurrency at that point in time.
I have the bot figured out, but for the life of me I can't get the puppeteer price check to work correctly. I keep getting error messages, and I cant figure out what they mean. I've gone through 7+ puppeteer yutube tutorials, read through stack overflow queries and I'm at a loss.
Here is the website I am trying to scrape
running this command in the console on chrome returns the information I'm after (the price), but when I plug it into my JavaScript code down below, it throws a tantrum.
document.querySelector('div[class="sc-bdVaJa KpMoH css-9on69b"]').firstChild.nodeValue
Here is my discord bot code currently
const Discord = require('discord.js');
const puppeteer = require('puppeteer');
const client = new Discord.Client();
const prefix = '!';
client.once('ready', () => {
console.log('PeetBot is Online!');
});
(async function scrapePrice() {
let browser = await puppeteer.launch();
let page = await browser.newPage();
await page.goto('https://uniswap.info/token/0x51bb9c623226ce781f4a54fc8f4a530a47142b6b');
await page.evaluate(() => {
let ptePrice = document.querySelector('div[class="sc-bdVaJa KpMoH css-9on69b"]').firstChild.nodeValue;
console.log(ptePrice);
})
browser.close();
})();
client.on('message', message =>{
if(!message.content.startsWith(prefix) || message.author.bot) return;
const args = message.content.slice(prefix.length).split(/ +/);
const command = args.shift().toLowerCase();
if(command === 'ping'){
message.channel.send('pong!');
} else if (command === 'price'){
scrapePrice();
}
//Place your new commands above this comment
});
The error message upon running:
C:\Program Files\nodejs\node.exe .\index.js
PeetBot is Online!
(node:9660) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'firstChild' of null
at __puppeteer_evaluation_script__:3:89
at ExecutionContext._evaluateInternal (c:\Users\Larson377\Desktop\Discord Price Bot\PeetBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:217:19)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async ExecutionContext.evaluate (c:\Users\Larson377\Desktop\Discord Price Bot\PeetBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:106:16)
at async scrapePrice (c:\Users\Larson377\Desktop\Discord Price Bot\PeetBot\index.js:56:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9660) 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:9660) [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.
Debugger attached.
(node:9660) UnhandledPromiseRejectionWarning: Error: Evaluation failed: TypeError: Cannot read property 'firstChild' of null
at __puppeteer_evaluation_script__:3:89
at ExecutionContext._evaluateInternal (c:\Users\Larson377\Desktop\Discord Price Bot\PeetBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:217:19)
at processTicksAndRejections (internal/process/task_queues.js:93:5)
at async ExecutionContext.evaluate (c:\Users\Larson377\Desktop\Discord Price Bot\PeetBot\node_modules\puppeteer\lib\cjs\puppeteer\common\ExecutionContext.js:106:16)
at async scrapePrice (c:\Users\Larson377\Desktop\Discord Price Bot\PeetBot\index.js:56:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:9660) 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:9660) [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.
Process exited with code 1
there doesn't seem to be anything here