I want to scrape some vessel details using Node Js and Puppeteer. When I try to scrape data , the result is showing null. I don't have any idea about why I am getting output like this. Here is my code.
index.js
const puppeteer = require("puppeteer");
const Scraper = require("./scrapers/class-based");
/**
* Run Scraper
*/
(async () => {
let browser;
let page;
try {
browser = await puppeteer.launch({
headless: false
});
page = await browser.newPage();
await new Scraper(browser, page).main();
} catch (error) {
console.log(error.stack || error);
}
await browser.close();
})();
class.js
const writeFileSync = require("fs").writeFileSync;
/**
* @class Scraper
*/
module.exports = class Scraper {
/**
* @constructor
*/
constructor(browser, page) {
this.browser = browser;
this.page = page;
this.standings = [];
this.url = "https://www.myshiptracking.com/vessels";
}
// @method main
async main() {
await this.page.goto(this.url, { waitUntil: "domcontentloaded" });
this.standings = await this.page.evaluate(() =>
Array.from(document.querySelectorAll("tbody > tr")).map(team => [
team.querySelector("td:nth-child(4)").getAttribute("data-value"),
team.querySelector("td:nth-child(6)").getAttribute("data-value")
])
);
this.writeToJson();
}
/**
* @method writeToJson
*/
writeToJson() {
writeFileSync("./data/standings.json", JSON.stringify(this.standings));
}
};
output: Standings.json
[[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null],[null,null]]
I am new to Node JS and Scraping. I followed tutorial and got answer like this. If anyone could help it would be really helpful. Thanks.