Helloo! I have been trying to make a Walmart stock monitor, but I ran into an issue, but realized that redirecting to the desired link again fixes the issue therefore in my code. I try to catch the error that lets me identify the error and then run the original function again, but with the parameter that sets a=1
so that it triggers the if statement causing it to refresh to go to the desired page and check stock again, but its not working how I desired, it appears that the parameter a
always gets set to 0
and I have no clue why, id gladly appreciate any help !
const core = require('puppeteer-core');
async function init(a) {
let b = a;
console.log(b);
const browser = await core.launch({
executablePath: '/Program Files/Google/Chrome/Application/chrome.exe',
headless: false,
defaultViewport: null,
args: [
'--user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36',
'--accept-language=en-US,en;q=0.9',
'--sec-fetch-site=none',
],
});
const page = (await browser.pages())[0];
await page.goto(
'https://www.walmart.com/ip/POP-Rocks-BTS-Jungkook/406893736'
);
if (a == 1) {
reload(page);
stockChecker();
console.log(a);
} else {
console.log(`testing...${a}`);
stockChecker(page);
console.log('LfdlA');
}
}
async function stockChecker(page) {
let stock = await page.evaluate(() => {
let in_out_of_stock = document.querySelector(
'link[itemprop="availability"]'
).href;
return in_out_of_stock;
});
let seller = await page.evaluate(() => {
let seller = document.querySelector('.seller-name').innerText;
return seller;
});
if (
stock.toLowerCase().includes('instock') &&
seller.toLowerCase().includes('walmart')
) {
return console.log('in stock');
} else {
return console.log('out of stock');
}
}
async function reload(page) {
await page.goto(
'https://www.walmart.com/ip/POP-Rocks-BTS-Jungkook/406893736'
);
}
async function run() {
let a = 0;
try {
await init(a);
} catch (e) {
if (e.toString().toLowerCase().includes("'href' of null")) {
a = 1;
init(a);
console.log(a);
} else {
console.log(e.toString());
}
}
}
run();
Also I am very new to using puppeteer if you recommend any other web automation frameworks that are better in terms of speed / effectiveness or see any ways for me to refactor my code id gladly appreciate that as well comrades.
Aucun commentaire:
Enregistrer un commentaire