-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrape.js
More file actions
188 lines (185 loc) · 5.74 KB
/
scrape.js
File metadata and controls
188 lines (185 loc) · 5.74 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
const puppeteer = require('puppeteer')
const readline = require('readline')
const WAIT_TIME = 800
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
crlfDelay: Infinity
})
async function scrape (url, username, password) {
const browser = await puppeteer.launch({ headless: false })
const page = await browser.newPage()
await page.goto(url)
const agree = await page.$x('/html/body/div[2]/div/div/div/div[2]/button[1]')
await agree[0].click()
await page.waitForSelector('input[name=username]')
await page.type('input[name=username]', username)
await page.type('input[name=password]', password)
const login = await page.$x('//*[@id="loginForm"]/div/div[3]/button')
await login[0].click()
await page.waitForXPath('/html/body/div[4]/div/div/div/div[3]/button[2]')
const nofs = await page.$x('/html/body/div[4]/div/div/div/div[3]/button[2]')
await nofs[0].click()
await page.goto('https://www.instagram.com/benqasas')
await page.waitForXPath(
'//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a'
)
const followers = await page.$x(
'//*[@id="react-root"]/section/main/div/header/section/ul/li[2]/a'
)
const followerscount = await followers[0].getProperty('textContent')
const follintstr = followerscount._remoteObject.value
const follint = follintstr.split(' ')
await followers[0].click()
var followersarr = []
const jqa = 'html > body > div > div > div > div:nth-child(2)'
const folldiv = await page.$(
'html > body > div > div > div > div:nth-child(2)'
)
var count = follint[0] * 54
var pastscrollheight = 0
var iai = 0
await page.waitForTimeout(WAIT_TIME)
follint[0] = +5 + +follint[0]
while (1) {
await page.evaluate(async () => {
document
.querySelector('html > body > div > div > div > div:nth-child(2)')
.scrollBy(0, 400)
})
const scrollheight = await page.evaluate(async () => {
return document.querySelector(
'html > body > div > div > div > div:nth-child(2) > ul'
).offsetHeight
})
if (scrollheight === pastscrollheight) {
iai++
} else {
iai = 0
}
if (iai >= WAIT_TIME) {
break
}
pastscrollheight = scrollheight
}
for (var i = 1; i <= follint[0]; i++) {
var a1 = await page.$x(
'/html/body/div[5]/div/div/div[2]/ul/div/li[' +
i.toString() +
']/div/div[1]/div[2]/div[1]/span/a'
)
if (typeof a1 === 'undefined' && a1.length === 0) {
a1 = await page.$x(
'/html/body/div[5]/div/div/div[2]/ul/div/li[' +
i.toString() +
']/div/div[1]/div[1]/div[1]/div/span/a'
)
}
try {
const a3 = await page.evaluate(element => element.textContent, a1[0])
followersarr.push(a3)
} catch {
if (+follint[0] - +5 >= follint[0]) {
console.log(`Failed to extract at following ${i.toString()}`)
}
}
}
page.waitForTimeout(WAIT_TIME)
await page.goto('https://www.instagram.com/benqasas')
const following = await page.$x(
'//*[@id="react-root"]/section/main/div/header/section/ul/li[3]/a'
)
const followingcount = await following[0].getProperty('textContent')
const folliwntstr = followingcount._remoteObject.value
const folliwnt = folliwntstr.split(' ')
await following[0].click()
var count = folliwnt[0] * 54
var pastscrollheight2 = 0
var iai2 = 0
var followingarr = []
await page.waitForTimeout(WAIT_TIME)
while (1) {
await page.evaluate(async () => {
document
.querySelector('html > body > div > div > div > div:nth-child(3)')
.scrollBy(0, 400)
})
const scrollheight = await page.evaluate(async () => {
return document.querySelector(
'html > body > div > div > div > div:nth-child(3) > ul'
).offsetHeight
})
if (scrollheight === pastscrollheight2) {
iai2++
} else {
iai2 = 0
}
if (iai2 >= WAIT_TIME) {
break
}
pastscrollheight2 = scrollheight
}
folliwnt[0] = +5 + +folliwnt[0]
for (var i = 1; i <= folliwnt[0]; i++) {
var a1 = await page.$x(
'/html/body/div[5]/div/div/div[2]/ul/div/li[' +
i.toString() +
']/div/div[1]/div[2]/div[1]/span/a'
)
if (typeof a1 === 'undefined' && a1.length === 0) {
a1 = await page.$x(
'/html/body/div[5]/div/div/div[2]/ul/div/li['+i.toString()+']/div/div[2]/div/div/div/span/a'
)
}
if (a1.length === 0) {
a1 = await page.$x(
'/html/body/div[5]/div/div/div[2]/ul/div/li[' +
i.toString() +
']/div/div[1]/div[2]/div[1]/span/a'
)
}
try {
const a3 = await page.evaluate(element => element.textContent, a1[0])
followingarr.push(a3)
} catch {
if (+folliwnt[0] - +5 >= folliwnt[0]) {
console.log(`Failed to extract at following ${i.toString()}`)
}
}
}
browser.close()
for (var i = 0; i < followingarr.length; i++) {
var isthere = 0
for (var j = 0; j < followersarr.length; j++) {
if (followersarr[j] === followingarr[i]) {
isthere = 1
break
}
}
if (isthere === 0) {
rl.write(`${followingarr[i]} doesn't follow you back \n`)
}
}
}
function GetInfo () {
var username
var password
rl.question('Enter instagram username: ', answer => {
username = answer
console.log('Enter instagram password: ')
rl.stdoutMuted = true
rl.question( "\n", function(answer2) {
password = answer2
rl.stdoutMuted = false
scrape('https://www.instagram.com', username, password)
rl.close()
})
})
}
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (rl.stdoutMuted && stringToWrite != '\r\n' && stringToWrite != '\n' && stringToWrite != '\r')
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
GetInfo()