Created
April 6, 2020 09:51
-
-
Save andriyor/94e5684e9f04a6d11bf72e3fe369ba1d to your computer and use it in GitHub Desktop.
This file contains 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'); | |
const scrape = async() => { | |
const browser = await puppeteer.launch({ headless: false, args: ['--start-maximized'] }); | |
const page = await browser.newPage(); | |
await page.setViewport({ width: 0, height: 0 }); | |
await page.goto('https://www.bookdepository.com/'); | |
await page.click(".sidebar .block-wrap a[href='/bestbooksever']"); | |
await page.waitForSelector('.book-item'); | |
const result = await page.evaluate(() => { | |
const data = []; // Создаём пустой массив для хранения данных | |
const elements = document.querySelectorAll('.book-item'); // Выбираем все товары | |
for (var element of elements) { // Проходимся в цикле по каждому товару | |
const title = element.querySelector('h3.title').innerText; // Выбираем название | |
const price = element.querySelector('div.price-wrap').innerText; // Выбираем цену | |
data.push({ title, price }); // Помещаем объект с данными в массив | |
} | |
return data; // Возвращаем массив | |
}); | |
// browser.close(); | |
return result; // Возвращаем данные | |
}; | |
scrape().then((value) => { | |
console.log(value); // Получилось! | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment