-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpuppeteer-scraper-cli.js
More file actions
209 lines (180 loc) · 7 KB
/
puppeteer-scraper-cli.js
File metadata and controls
209 lines (180 loc) · 7 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
const readline = require("readline");
const YellowPagesPuppeteerScraper = require("./puppeteer-scraper-module");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
function question(query) {
return new Promise((resolve) => {
rl.question(query, resolve);
});
}
// main function
async function main() {
console.log("\n========================================");
console.log(" ");
console.log(" 🍕 YellowPages Web Scraper 🍕");
console.log(" ");
console.log("========================================\n");
const scraper = new YellowPagesPuppeteerScraper();
try {
const query = await question(
"What are you looking for? (e.g., pizza, kung-fu lessons): "
);
const location = await question("Where? (e.g., 90210, Los Angeles, CA): ");
const defaultResults = 30;
const targetResultsInput = await question(
`How many results do you want to collect? (e.g., 10, Max: ${scraper.maxResultsLimit}): `
);
let targetResults;
if (!targetResultsInput.trim()) {
targetResults = defaultResults;
console.log(
`No number specified, using default of ${defaultResults} results.`
);
} else {
targetResults = parseInt(targetResultsInput, 10);
if (isNaN(targetResults) || targetResults <= 0) {
console.log(
"Invalid input. Please enter a positive number or press Enter for default."
);
rl.close();
return;
}
}
if (targetResults > scraper.maxResultsLimit) {
console.log(
`Requested results (${targetResults}) exceed the maximum limit (${scraper.maxResultsLimit}). The search will be capped at ${scraper.maxResultsLimit}.`
);
}
console.log(
`\nStarting scrape for "${query}" in "${location}" targeting ${Math.min(
targetResults,
scraper.maxResultsLimit
)} results...\n`
);
// Inter-page delay log
console.log(
`Using inter-page delays of ${2500 / 1000}-${5000 / 1000} seconds.`
);
const businesses = await scraper.search(query, location, targetResults);
if (businesses.length === 0) {
console.log("\nNo results found.");
rl.close();
return;
}
console.log(`\nCollected ${businesses.length} businesses:`);
console.log("==========================================");
const displayLimit = Math.min(businesses.length, 10);
console.log(`Displaying the first ${displayLimit} results:`);
businesses.slice(0, displayLimit).forEach((business, index) => {
console.log(`${index + 1}. ${business.businessName}`);
console.log(` Type: ${business.businessType}`);
console.log(` Phone: ${business.phone}`);
if (business.website) console.log(` Website: ${business.website}`);
console.log(` Address: ${business.streetAddress}`);
console.log(
` ${business.city}, ${business.state} ${business.zipCode}`
);
console.log("------------------------------------------");
});
if (businesses.length > displayLimit) {
console.log(`... and ${businesses.length - displayLimit} more results.`);
}
// generate default filenames
const jsonFilename = scraper.generateFilename(query, location, "json");
const csvFilename = scraper.generateFilename(query, location, "csv");
console.log("\nSave options:");
console.log(
`1. Save as JSON (new file: ${jsonFilename} in ${scraper.jsonDir} directory)`
);
console.log(
`2. Save as JSON (choose existing file to append in ${scraper.jsonDir} directory)`
);
console.log(
`3. Save as CSV (new file: ${csvFilename} in ${scraper.csvDir} directory)`
);
console.log(
`4. Save as CSV (choose existing file to append in ${scraper.csvDir} directory)`
);
console.log("5. Don't save");
const saveOption = await question("\nChoose an option (1-5): ");
let selectedFile = "";
switch (saveOption) {
case "1":
// save as new JSON file with generated filename
scraper.exportToJSON(businesses, jsonFilename, false);
break;
case "2":
// list existing JSON files and let user choose one to append to
const jsonFiles = scraper.listExistingFiles("json");
if (jsonFiles.length === 0) {
console.log(
`No existing JSON files found in ${scraper.jsonDir} directory. Creating new file instead.`
);
scraper.exportToJSON(businesses, jsonFilename, false);
break;
}
console.log(`\nExisting JSON files in ${scraper.jsonDir} directory:`);
jsonFiles.forEach((file, index) => {
console.log(`${index + 1}. ${file}`);
});
console.log(`${jsonFiles.length + 1}. Use new file (${jsonFilename})`);
const jsonFileChoice = await question(
`\nChoose a file (1-${jsonFiles.length + 1}): `
);
const jsonFileIndex = parseInt(jsonFileChoice, 10) - 1;
if (jsonFileIndex >= 0 && jsonFileIndex < jsonFiles.length) {
selectedFile = jsonFiles[jsonFileIndex];
scraper.exportToJSON(businesses, selectedFile, true);
} else {
// default to new file if invalid choice
console.log(`Using new file: ${jsonFilename}`);
scraper.exportToJSON(businesses, jsonFilename, false);
}
break;
case "3":
// save as new CSV file with generated filename
scraper.exportToCSV(businesses, csvFilename, false);
break;
case "4":
// list existing CSV files and let user choose one to append to
const csvFiles = scraper.listExistingFiles("csv");
if (csvFiles.length === 0) {
console.log(
`No existing CSV files found in ${scraper.csvDir} directory. Creating new file instead.`
);
scraper.exportToCSV(businesses, csvFilename, false);
break;
}
console.log(`\nExisting CSV files in ${scraper.csvDir} directory:`);
csvFiles.forEach((file, index) => {
console.log(`${index + 1}. ${file}`);
});
console.log(`${csvFiles.length + 1}. Use new file (${csvFilename})`);
const csvFileChoice = await question(
`\nChoose a file (1-${csvFiles.length + 1}): `
);
const csvFileIndex = parseInt(csvFileChoice, 10) - 1;
if (csvFileIndex >= 0 && csvFileIndex < csvFiles.length) {
selectedFile = csvFiles[csvFileIndex];
scraper.exportToCSV(businesses, selectedFile, true);
} else {
// default to new file if invalid choice
console.log(`Using new file: ${csvFilename}`);
scraper.exportToCSV(businesses, csvFilename, false);
}
break;
case "5":
console.log("Not saving.");
break;
default:
console.log("Invalid option. Not saving.");
}
} catch (error) {
console.error("An unexpected error occurred:", error.message);
} finally {
rl.close();
}
}
main();