-
Notifications
You must be signed in to change notification settings - Fork 0
/
almirah_items.js
51 lines (48 loc) · 1.74 KB
/
almirah_items.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const puppeteer = require('puppeteer');
const cheerio = require('cheerio');
(async () => {
const browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
page.setDefaultNavigationTimeout(0)
await page.goto("https://www.almirah.com.pk/")
await page.waitForSelector("div.text-center")
await page.screenshot({ path: "testing.png" })
await page.waitForSelector("div.text-center > ul li a")
await page.evaluate(() => {
const a = document.querySelectorAll("div.text-center > ul li a")
a[1].click()
})
await page.waitForNavigation()
await autoScroll(page)
const content = page.content()
content.then(success => {
const $ = cheerio.load(success)
let arr = $('div.grid-product__title').map(a => {
return{
title: $('div.grid-product__title')[a].children[0].data,
price: $('span.money')[a].children[0].data,
image: $('div.image-wrap > img')[a].attribs.srcset
}
})
console.log({arr})
})
await page.screenshot({path: 'testing.png'})
await browser.close()
})()
async function autoScroll(page){
await page.evaluate(async () => {
await new Promise((resolve, reject) => {
var totalHeight = 0;
var distance = 100;
var timer = setInterval(() => {
var scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if(totalHeight >= scrollHeight){
clearInterval(timer);
resolve();
}
}, 500);
});
});
}