Estoy tratando de seguir las pautas de un tutorial de web scrapping en Amazon, el cual utiliza pupeteer y cheerio.
Tengo las dependencias instaladas en node:
{
"name": "scrapy",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cheerio": "^1.0.0-rc.10",
"cron": "^1.8.2",
"nodemailer": "^6.6.2",
"puppeteer": "^10.1.0"
},
"devDependencies": {},
"description": ""
}
Ahora bien, consigo exportar 'cheerio' sin problema en una constante global llamada '$', tal y como viene en el tutorial. Igualmente traté de ponerle una palabra y sigue dando el fallo, así que deduzco que no es el caracter.
Aquí tengo el código:
const puppeteer = require('puppeteer');
const $ = require('cheerio');
const CronJob = require('cron').CronJob;
const nodemailer = require('nodemailer');
const url = 'https://www.amazon.com/Sony-Noise-Cancelling-Headphones-WHCH710N/dp/B085RNVJ3P/';
async function configureBrowser(){
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto(url);
return page;
}
async function checkPrice(page){
await page.reload();
let html = await page.evaluate(() => document.body.innerHTML);
//console.log(html);
$('#priceblock_saleprice', html).each(function(){
let dollarPrice = $(this).text();
console.log(dollarPrice)
});
}
async function monitor(){
let page = await configureBrowser();
await checkPrice(page);
}
monitor();
Y aquí pongo el log del error:
(node:11639) UnhandledPromiseRejectionWarning: TypeError: $ is not a function
at checkPrice (/home/linux3/Escritorio/www/scrapy/index.js:20:5)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async monitor (/home/linux3/Escritorio/www/scrapy/index.js:28:6)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:11639) 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:11639) [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.
No entiendo qué puede ser, ya que sigo el github igual excepto el scrap de la web, ya que ellos usan amazon en
y yo es
.