Skip to content

Instantly share code, notes, and snippets.

@kalmanh
Forked from bmorelli25/allScrape.js
Last active December 3, 2017 05:54
Show Gist options
  • Save kalmanh/5c6adc770c1f850652812fc4ccbcfae2 to your computer and use it in GitHub Desktop.
Save kalmanh/5c6adc770c1f850652812fc4ccbcfae2 to your computer and use it in GitHub Desktop.
Scrape all books on the homepage - without a for loop
const puppeteer = require('puppeteer');
let scrape = async () => {
const browser = await puppeteer.launch({headless: true});
const page = await browser.newPage();
await page.goto('http://books.toscrape.com/');
const result = await page.evaluate(() => {
let elements = document.querySelectorAll('.product_pod'); // Select all Products
return Array.from(elements).reduce((accum, element) => {
let title = element.querySelector('div a img').getAttribute('alt'); // Select the title
let price = element.querySelector('div.product_price p.price_color').innerText; // Select the price
accum.push({title, price}); // Push an object with the data onto our array
return accum;
}, []);
});
browser.close();
return result; // Return the data
};
scrape().then((value) => {
console.log(value); // Success!
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment