-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscraper.js
48 lines (44 loc) · 1.8 KB
/
scraper.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
const puppeteer = require("puppeteer")
const BadRequestError = require("./errors/custom-error")
const scraper = async (req, res) => {
const { username, email, password } = req.body
if(!username || !email || !password) {
throw new BadRequestError("Please provide required information !")
}
const browser = await puppeteer.launch({headless: false})
const page = await browser.newPage()
await page.goto("https://www.instagram.com/")
await page.type('input[name="username"]',email)
await page.type('input[name="password"]',password)
await page.click('button[type="submit"]')
await page.waitForNavigation()
await page.goto(`https://www.instagram.com/${username}/`)
await page.waitForSelector("._ac2a")
const info = await page.evaluate(() => {
const info = document.querySelectorAll("._ac2a")
const posts = info[0].textContent
const followers = info[1].textContent
const following = info[2].textContent
return { posts, followers, following }
})
const numFollowers = parseInt(info.followers)
await page.click(`a[href="/${username}/followers/"]`)
await page.waitForSelector("span ._ab8y")
let hoverOverThis = `._aano > div > div > div:last-child`
for(let i=1; i< Math.round(numFollowers/10); i++) {
await page.waitForSelector(hoverOverThis);
await page.hover(hoverOverThis)
await page.waitForTimeout(1500)
}
const followers = await page.evaluate(async () => {
const followers = document.querySelectorAll("span ._ab8y")
let names = []
for (let i = 0; i < followers.length; i++) {
names.push(followers[i].textContent)
}
return names
})
await browser.close()
res.status(200).json({info, followers})
}
module.exports = scraper