-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.js
More file actions
83 lines (65 loc) · 1.96 KB
/
scraper.js
File metadata and controls
83 lines (65 loc) · 1.96 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
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var scraper = {
get_data_from_url: function(id_string, callback) {
var full_url = "http://www.tfrrs.org/athletes/" + id_string;
// strip trailing ".html" if present
if (id_string.slice(-5) == ".html") {
id_string = id_string.slice(0,-5);
}
var id = parseInt(id_string);
// scrape
return request.get(full_url, function(error, response, html) {
var data = {};
if(!error){
var $ = cheerio.load(html);
data.athlete = this.scrape_athlete_page($);
data.athlete.id = id;
data.athlete.url = full_url;
} else {
data.error = "error getting tfrrs page.";
}
callback(data);
}.bind(this));
},
scrape_athlete_page: function($) {
var athlete = {};
var athlete_container = $("form[name='athlete']");
var title = athlete_container.find("h3.panel-title").first();
athlete.name = title.text().trim();
athlete.races = [];
// Select each meet
$("#meet-results table").each(function() {
// The HTML structure looks like this (as of March 2018)
// #meet-results
// table
// thead
// tr
// a (Meet Name)
// span (Date)
// tr
// td (Event)
// td (Mark/Time)
// td (Place)
// tr (Repeat for each race at the meet)
// ...
var meet = $(this).find("thead a").text().trim();
var date = $(this).find("thead span").text().trim();
// Select each performance
$(this).children("tr").each(function() {
var columns = $(this).find("td");
var race = {
"date": date,
"meet": meet,
"event": columns.eq(0).text().trim(),
"mark": columns.eq(1).text().trim(),
"place": columns.eq(2).text().trim()
};
athlete.races.push(race);
});
});
return athlete;
}
};
module.exports = scraper;