-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexWithPooling.js
229 lines (195 loc) · 8.91 KB
/
indexWithPooling.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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
const puppeteer = require('puppeteer');
const fs = require('fs');
const { MultiBar, Presets } = require('cli-progress');
// Function to create a new page or get a page from the pool
async function getPageFromPool(browser, pagePool) {
if (pagePool.length > 0) {
return pagePool.pop();
} else {
return await browser.newPage();
}
}
// Function to release a page back to the pool
function releasePageToPool(page, pagePool) {
pagePool.push(page);
}
// Function to scroll down the page and grab xxxx number of person links
async function scrollAndGrabLinks(page) {
const maxScrollAttempts = 655; // Adjust this value based on the website's behavior
const scrollTimeout = 3000; // Adjust the waiting time based on website responsiveness
const maxPersonLinks =8810;
const personLinks = [];
const multiBar = new MultiBar(
{
format: 'Scanning |' + '{bar}' + '| {percentage}% | ETA: {eta}s | {value}/{total}',
hideCursor: true,
},
Presets.shades_grey
);
const progressBar = multiBar.create(maxScrollAttempts, 0);
try {
for (let scrollAttempt = 0; scrollAttempt < maxScrollAttempts; scrollAttempt++) {
await page.evaluate('window.scrollTo(0, document.body.scrollHeight);');
await page.waitForTimeout(scrollTimeout);
// Extract person links
const newLinks = await page.$$eval('.avatar a', (links) => links.map((link) => link.href));
// Filter and add unique links to the personLinks array
for (const link of newLinks) {
if (personLinks.length >= maxPersonLinks) {
return personLinks;
}
if (!personLinks.includes(link)) {
personLinks.push(link);
}
}
progressBar.update(scrollAttempt + 1);
}
} catch (error) {
console.error('Error occurred while scrolling and grabbing person links:', error.message);
} finally {
multiBar.stop();
}
return personLinks;
}
// Function to scroll down the page and grab links after a specific href link is loaded
async function scrollAndGrabLinksAfterHref(page, targetHref) {
const maxScrollAttempts = 500; // Adjust this value based on the website's behavior
const scrollTimeout = 3000; // Adjust the waiting time based on website responsiveness
const linksAfterTarget = [];
let targetLinkLoaded = false;
// Create a new progress bar
const multiBar = new MultiBar(
{
format: 'Scanning |' + '{bar}' + '| {percentage}% | ETA: {eta}s | {value}/{total}',
hideCursor: true,
},
Presets.shades_grey
);
// Create a new progress bar item
const progressBar = multiBar.create(maxScrollAttempts, 0);
try {
for (let scrollAttempt = 0; scrollAttempt < maxScrollAttempts; scrollAttempt++) {
await page.evaluate('window.scrollTo(0, document.body.scrollHeight);');
await page.waitForTimeout(scrollTimeout);
// Check if the targetHref link is loaded
if (!targetLinkLoaded) {
targetLinkLoaded = await page.evaluate((targetHref) => {
const targetLink = document.querySelector(`a[href="${targetHref}"]`);
return !!targetLink;
}, targetHref);
}
// If the target link is loaded, start extracting links that follow it
if (targetLinkLoaded) {
console.log('Target link is loaded. Now extracting links after it...');
const newLinks = await page.$$eval('.avatar a', (links) => links.map((link) => link.href));
linksAfterTarget.push(...newLinks);
}
// Update the progress bar
progressBar.update(scrollAttempt + 1);
// Break the loop if the target link is loaded and all links have been collected
if (targetLinkLoaded && linksAfterTarget.length >= 3500) {
break;
}
}
console.log('\nReached the end of the page or collected all available links after the target link.');
} catch (error) {
console.error('Error occurred while scrolling and grabbing links:', error.message);
} finally {
// Stop and remove the progress bar
multiBar.stop();
}
return linksAfterTarget;
}
// Function to scrape data from the website
async function scrapeData() {
let browser;
try {
browser = await puppeteer.launch({
headless: false,
ignoreHTTPSErrors: true,
protocolTimeout: 180000,
//executablePath: 'C:\\Program Files\\Google\\Chrome\Application\\chrome.exe', // <-- Add your Chrome path here
args: [
'--disable-defaults-apps',
'--enable-gpu',
'--use-gl=egl',
'--aggressive-cache-discard',
'--disable-cache',
'--disable-application-cache',
'--disable-offline-load-stale-cache',
'--media-cache-size=0',
'--disk-cache-size=0',
'--js-flags=--max_old_space_size=8192',
],
});
const page = await browser.newPage();
// Navigate to the login page
await page.goto('https://10ksbconnect.com/login', { waitUntil: 'networkidle2' });
// Now you can scrape data from the authenticated page with Infinite Scroll
await page.type('#mat-input-0', 'enter page email');
await page.type('#mat-input-1', 'Enter page password');
// Submit the login form
await page.click('.mat-focus-indicator');
console.log("Logged in");
await page.waitForNavigation();
// Now you can scrape data from the authenticated page with Infinite Scroll
await page.goto('https://10ksbconnect.com/directory', { waitUntil: 'networkidle2'});
console.log("Navigated to directory page");
// Page pool and visitedUrls set
const pagePool = [];
const visitedUrls = new Set();
// Scroll down the page and grab xxxx person links
console.log('Scrolling started....');
console.time('myTimer');
//const targetHref = 'https://10ksbconnect.com/user/784167'; // <-- Add the href of the last person link on the page https://10ksbconnect.com/user/784167
//const linksAfterTarget = await scrollAndGrabLinksAfterHref(page, targetHref);
const personLinks = await scrollAndGrabLinks(page);
const personData = [];
console.log('Finished scrolling....');
console.timeEnd('myTimer');
console.log('Total number of persons found:', personLinks.length); // or linksAfterTarget
// Page pooling and data extraction
console.time('myTimer');
for (const personUrl of personLinks) { // or linksAfterTarget
if (!visitedUrls.has(personUrl)) {
visitedUrls.add(personUrl);
const personPage = await getPageFromPool(browser, pagePool);
await personPage.goto(personUrl, { waitUntil: 'networkidle0', timeout: 90000 });
const personInfo = await personPage.evaluate(() => {
const nameElement = document.querySelector('#viewUserProfileFullName');
const emailElement = document.querySelector('#viewUserProfileEmail');
const phoneElement = document.querySelector('#viewUserProfilePhone');
const addressElement = document.querySelector('#viewUserProfileSchoolEducation');
const businessNameElement = document.querySelector('#viewUserProfileCompanyName');
const jobTitleElement = document.querySelector('#viewUserProfileJobTitle');
const industryElement = document.querySelector('#viewUserProfileIndustryField');
const websiteElement = document.querySelectorAll('.value a');
const thirdElement = websiteElement[2];
const name = nameElement ? nameElement.textContent.trim() : 'Not available';
const email = emailElement ? emailElement.textContent.trim() : 'Not available';
const phone = phoneElement ? phoneElement.textContent.trim() : 'Not available';
const address = addressElement ? addressElement.textContent.trim() : 'Not available';
const businessName = businessNameElement ? businessNameElement.textContent.trim() : 'Not available';
const jobTitle = jobTitleElement ? jobTitleElement.textContent.trim() : 'Not available';
const industry = industryElement ? industryElement.textContent.trim() : 'Not available';
const website = thirdElement ? thirdElement.textContent.trim() : 'Not available';
return { name, email, phone, website, address, businessName, jobTitle, industry, personPageUrl: window.location.href};
});
personData.push(personInfo);
releasePageToPool(personPage, pagePool);
}
}
console.timeEnd('myTimer');
console.log('Total number of persons added to personData:', personData.length);
// Save person data to a JSON file
const jsonData = JSON.stringify(personData, null, 2);
fs.writeFileSync('contact_data1_3.json', jsonData);
console.log('Contact data saved to contact_data1_2.json');
} catch (error) {
console.error('Error occurred while scraping data:', error.message);
} finally {
if (browser) {await browser.close();}
}
}
// Call the scrapeData function
scrapeData();