-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
51 lines (40 loc) · 1.02 KB
/
app.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
const rp = require('request-promise');
const cheerio = require('cheerio');
const fs = require('fs');
function append(file, data){
fs.appendFile(file, data.join('\n'),'utf8', function (err) {
if (err) throw err;
console.log('Data appended!');
});
}
function filterUrls($){
const hyper = $('a[href]');
hyper.each((index, elem)=>{
scrapedData.push($(elem).attr('href'));
});
}
async function scrape(uri){
var options = {
uri,
transform: function(body){
return cheerio.load(body);
}
};
try {
const $ = await rp(options);
filterUrls($);
} catch(err){
console.log(err);
}
}
const urlArray = process.argv.slice(2);
let scrapedData = [];
let promisesArray = [];
urlArray.map(url => {
promisesArray.push(scrape(url));
});
Promise.all(promisesArray).then(() => {
const filteredData = scrapedData.filter(url=>
['http','www'].some(el => url.includes(el)));
append("urls.txt",filteredData);
});