I am having a problem where I can only see the contents of my output array vars from inside of my main Puppeteer function scrapeEbay().
Is this because this function is asynchronous? My intent is to have the main function be able to see the contents of the output arrays; to return them. But they will show up empty, unless log those arrays inside of the scrapeEbay() function.
Code below, just curious as to what the reasoning is behind this?
var outputPrices = [];
var outputItems = [];
async function scrapeEbay(inputString) {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
// Open advanced search on ebay
await page.goto("https://ebay.com/sch/ebayadvsearch");
await page.type("#_nkw", inputString);
// Make Selections for conditon and sold status
await page.click("#LH_Sold");
await page.click("#LH_ItemConditionUsed");
// Get rid of international listings
await page.click("#LH_LocatedInRadio");
// submit ebay advanced search form
await page.click("#searchBTNLowerLnk");
await page.waitForSelector("span.s-item__price");
const scrapedPrices = await page.$$eval("span.s-item__price", (spans) => {
return [...spans].slice(1).map((span) => {
// https://stackoverflow.com/a/42309034
// Slice this string to get the desired pricing; instead of the daughter <span /> tag
var slicedNumber = span.innerHTML.slice(24, -7);
// remove Commas from numbers; to not mess with the parseFloat function below
var splitNumber = slicedNumber.replace(",", "");
var price = parseFloat(splitNumber);
return price;
});
});
outputItems.push(inputString);
outputPrices.push(median(scrapedPrices));
await browser.close();
console.log([outputItems], [outputPrices]);
}
async function main(inputArray) {
for (let i = 0; i < inputArray.length; i++) {
scrapeEbay(inputArray[i]);
}
console.log([outputItems], [outputPrices]);
}
main(["i7 6700k", "gtx 970"]);