As a personal challenge, I am trying to create a tool that would scrape the search results of a website (shopping platform AliBaba used for this experiment) using Puppeteer, and save the outputs into a JSON object, that can be later used to create a visualisation on the front-end.
My first step has been to access the first page of the search results, and scrape the listings from there into an array:
const puppeteer = require('puppeteer');
const fs = require('fs');
/* First page search URL */
const url = (keyword) => `https://www.alibaba.com/trade/search?fsb=y&IndexArea=product_en&CatId=&SearchText=${keyword}`
/* keyword to search for */
const keyword = `future`;
(async () => {
try {
const browser = await puppeteer.launch({
headless: true
});
const page = await browser.newPage();
await page.goto(url(keyword), {
waitUntil: 'networkidle2'
});
await page.waitForSelector('.m-gallery-product-item-v2');
let urls = await page.evaluate(() => {
let results = [];
let items = document.querySelectorAll('.m-gallery-product-item-v2');
// This console.log never gets printed to either the browser window or the terminal?
console.log(items)
items.forEach( item => {
let CurrentTime = Date.now();
let title = item.querySelector('h4.organic-gallery-title__outter').getAttribute("title");
let link = item.querySelector('.organic-list-offer__img-section').getAttribute("href");
let img = item.querySelector('.seb-img-switcher__imgs').getAttribute("data-image");
results.push({
'scrapeTime': CurrentTime,
'title': title,
'link': `https:${link}`,
'img': `https:${img}`,
})
});
return results;
})
console.log(urls)
browser.close();
} catch (e) {
console.log(e);
browser.close();
}
})();
When I run the file (test-2.js) in the terminal using Node, it sometimes returns the results array just fine, and on other times, throws an error. The terminal error that is thrown around half of the times is:
Error: Evaluation failed: TypeError: Cannot read property 'getAttribute' of null
at __puppeteer_evaluation_script__:11:82
at NodeList.forEach (<anonymous>)
at __puppeteer_evaluation_script__:8:19
at ExecutionContext._evaluateInternal (/Users/dmnk/scraper/node_modules/puppeteer/lib/ExecutionContext.js:102:19)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async ExecutionContext.evaluate (/Users/dmnk/scraper/node_modules/puppeteer/lib/ExecutionContext.js:33:16)
at async /Users/dmnk/scraper/test-2.js:24:20
-- ASYNC --
at ExecutionContext.<anonymous> (/Users/dmnk/scraper/node_modules/puppeteer/lib/helper.js:94:19)
at DOMWorld.evaluate (/Users/dmnk/scraper/node_modules/puppeteer/lib/DOMWorld.js:89:24)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
-- ASYNC --
at Frame.<anonymous> (/Users/dmnk/scraper/node_modules/puppeteer/lib/helper.js:94:19)
at Page.evaluate (/Users/dmnk/scraper/node_modules/puppeteer/lib/Page.js:612:14)
at Page.<anonymous> (/Users/dmnk/scraper/node_modules/puppeteer/lib/helper.js:95:27)
at /Users/dmnk/scraper/test-2.js:24:31
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:53159) UnhandledPromiseRejectionWarning: ReferenceError: browser is not defined
at /Users/dmnk/scraper/test-2.js:52:9
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:53159) 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:53159) [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.
I am relatively fresh to grasping and learning asynchronous JavaScript.
I have been trying for days to understand why this error happens, to no avail. Extremely thankful for any help in understanding the cause/troubleshooting.