-
-
Save kalmanh/5c6adc770c1f850652812fc4ccbcfae2 to your computer and use it in GitHub Desktop.
Scrape all books on the homepage - without a for loop
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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