-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01-scraping-api.js
More file actions
49 lines (40 loc) · 1.03 KB
/
01-scraping-api.js
File metadata and controls
49 lines (40 loc) · 1.03 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
const axios = require('axios').default;
const cheerio = require('cheerio');
const URL = 'https://www.ncaa.com/rankings/football/fbs/associated-press';
const PROPS = {
0: 'rank',
1: 'school',
2: 'record',
3: 'points',
4: 'previous',
};
const getProp = (i) => PROPS[i];
const fetchHtml = async (url) => {
try {
const { data } = await axios.get(url);
return data;
} catch {
console.error(
`ERROR: An error occurred while trying to fetch the URL: ${url}`
);
}
};
const parseResult = ($, element) => {
const result = {};
$(element)
.find('td')
.each((i, elm) => (result[getProp(i)] = $(elm).text()));
return result;
};
const prettyPrint = (result) =>
`${result.school} with a ${result.record} record`;
const scrape = async () => {
const html = await fetchHtml(URL);
const $ = cheerio.load(html);
const results = $('#block-bespin-content')
.find('tbody > tr')
.toArray()
.map((element) => parseResult($, element))
.map((element) => prettyPrint(element));
return results;
};