forked from abdulwasay8126/apollo-scraper-apify
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-local.js
More file actions
216 lines (173 loc) · 7.84 KB
/
test-local.js
File metadata and controls
216 lines (173 loc) · 7.84 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
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
/**
* Local testing script for Apollo.io scraper
* Run this to test the scraper locally before deploying to Apify
*
* Usage:
* node test-local.js
*
* Or with custom input:
* node test-local.js --url "https://app.apollo.io/#/people?page=1" --pages 2
*/
import { chromium } from 'playwright';
// Parse command line arguments
const args = process.argv.slice(2);
const getArg = (name, defaultValue) => {
const index = args.indexOf(name);
return index !== -1 && args[index + 1] ? args[index + 1] : defaultValue;
};
const input = {
url: getArg('--url', 'https://app.apollo.io/#/people?page=1'),
numberOfPages: parseInt(getArg('--pages', '2')),
timeBetweenPages: parseInt(getArg('--delay', '5'))
};
console.log('🚀 Starting local test...');
console.log('Input:', JSON.stringify(input, null, 2));
// Extract table data function (same as in main.js)
async function extractTableData(page) {
return await page.evaluate(() => {
const table = document.querySelector('table');
if (!table) {
return [];
}
const rows = Array.from(table.querySelectorAll('tr'));
const data = [];
for (let i = 1; i < rows.length; i++) {
const cells = Array.from(rows[i].querySelectorAll('td'));
if (cells.length === 0) continue;
const rowData = {};
cells.forEach((cell, index) => {
const clonedCell = cell.cloneNode(true);
const elementsToRemove = clonedCell.querySelectorAll('svg, img, button, input[type="checkbox"]');
elementsToRemove.forEach(el => el.remove());
let text = clonedCell.textContent.trim();
const phoneRegex = /\+\d{11}/g;
const phoneMatches = text.match(phoneRegex);
if (phoneMatches) {
phoneMatches.forEach(match => {
const formatted = match.replace(/(\+\d{1})(\d{3})(\d{3})(\d{4})/, '$1 ($2) $3-$4');
text = text.replace(match, formatted);
});
}
text = text.replace(/[^a-zA-Z0-9\s,.@()-]/g, '').replace(/Â/g, '').trim();
switch(index) {
case 0: break;
case 1:
if (text && text !== 'Name') {
const names = text.split(' ');
rowData.firstName = names[0] || '';
rowData.lastName = names.slice(1).join(' ') || '';
rowData.fullName = text;
}
break;
case 2: rowData.title = text || ''; break;
case 3: rowData.company = text || ''; break;
case 4:
if (text && text !== 'No email' && text !== 'NA') {
rowData.email = text;
}
break;
case 5:
if (text && text !== 'Request Mobile Number' && text !== 'NA') {
rowData.phone = text;
}
break;
default:
rowData[`column_${index}`] = text;
}
});
if (rowData.fullName || rowData.email) {
data.push(rowData);
}
}
return data;
});
}
// Main test function
async function runTest() {
const { url, numberOfPages, timeBetweenPages } = input;
// Validate
if (!url.includes('https://app.apollo.io/')) {
throw new Error('URL must be a valid Apollo.io list URL');
}
const maxPages = Math.min(numberOfPages, 100);
console.log(`\n📊 Configuration:`);
console.log(` URL: ${url}`);
console.log(` Pages: ${maxPages}`);
console.log(` Delay: ${timeBetweenPages}s\n`);
// Launch browser (headless = false for testing, so you can see what's happening)
console.log('🌐 Launching browser...');
const browser = await chromium.launch({
headless: false, // Set to true for headless mode
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
try {
const context = await browser.newContext({
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
});
const page = await context.newPage();
// Generate page URLs
const pageUrls = [];
let baseUrl = url.replace(/&page=\d+/, '').replace(/\?page=\d+/, '');
for (let i = 1; i <= maxPages; i++) {
const separator = baseUrl.includes('?') ? '&' : '?';
const pageUrl = `${baseUrl}${separator}page=${i}`;
pageUrls.push(pageUrl);
}
let allData = [];
// Scrape each page
for (let i = 0; i < pageUrls.length; i++) {
const pageUrl = pageUrls[i];
console.log(`\n📄 Page ${i + 1}/${pageUrls.length}`);
console.log(` URL: ${pageUrl}`);
try {
await page.goto(pageUrl, {
waitUntil: 'networkidle',
timeout: 60000
});
console.log(' ⏳ Waiting for table...');
await page.waitForSelector('table', { timeout: 30000 });
await page.waitForTimeout(3000);
const pageData = await extractTableData(page);
console.log(` ✅ Extracted ${pageData.length} contacts`);
// Show first contact as sample
if (pageData.length > 0) {
console.log(' Sample:', JSON.stringify(pageData[0], null, 2));
}
allData = allData.concat(pageData);
if (i < pageUrls.length - 1) {
console.log(` ⏸️ Waiting ${timeBetweenPages}s...`);
await page.waitForTimeout(timeBetweenPages * 1000);
}
} catch (error) {
console.error(` ❌ Error: ${error.message}`);
}
}
// Results
console.log(`\n\n🎉 Test Complete!`);
console.log(`═══════════════════════════════════════`);
console.log(`📊 Total contacts scraped: ${allData.length}`);
console.log(`📄 Pages scraped: ${pageUrls.length}`);
console.log(`⏱️ Average per page: ${(allData.length / pageUrls.length).toFixed(1)}`);
// Data quality stats
const withEmail = allData.filter(c => c.email).length;
const withPhone = allData.filter(c => c.phone).length;
console.log(`\n📧 Contacts with email: ${withEmail} (${(withEmail/allData.length*100).toFixed(1)}%)`);
console.log(`📱 Contacts with phone: ${withPhone} (${(withPhone/allData.length*100).toFixed(1)}%)`);
// Save to JSON file
const fs = await import('fs');
const filename = `test-results-${Date.now()}.json`;
fs.writeFileSync(filename, JSON.stringify(allData, null, 2));
console.log(`\n💾 Results saved to: ${filename}`);
console.log(`\n✅ Test successful! Ready to deploy to Apify.`);
} catch (error) {
console.error('\n❌ Test failed:', error);
throw error;
} finally {
await browser.close();
}
}
// Run the test
runTest().catch(error => {
console.error('Fatal error:', error);
process.exit(1);
});