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
|
# File 'lib/football_stats_filter_tool/data_scraper.rb', line 4
def self.scrape_data(url, stat_type)
html = Nokogiri::HTML(URI.open(url))
data = html.css('tr:not(.thead)')
out = []
data.each do |entry|
name = entry.css('td[data-stat="player"]').text
hash = {}
if(name != "")
hash[:name] = entry.css('td[data-stat="player"]').text.match(/.*[a-zA-Z]/)[0] hash[:age] = entry.css('td[data-stat="age"]').text.to_i
hash[:position] = entry.css('td[data-stat="pos"]').text.upcase
hash[:team] = entry.css('td[data-stat="team"]').text
if(stat_type == "passing")
hash[:pass_completions] = entry.css('td[data-stat="pass_cmp"]').text.to_f
hash[:pass_attempts] = entry.css('td[data-stat="pass_att"]').text.to_f
hash[:pass_completion_percentage] = entry.css('td[data-stat="pass_cmp_perc"]').text.to_f
hash[:pass_touchdowns] = entry.css('td[data-stat="pass_td"]').text.to_f
hash[:interceptions_thrown] = entry.css('td[data-stat="pass_int"]').text.to_f
hash[:pass_yards] = entry.css('td[data-stat="pass_yds"]').text.to_f
hash[:yards_per_pass_attempt] = entry.css('td[data-stat="pass_yds_per_att"]').text.to_f
elsif(stat_type == "rushing")
hash[:carries] = entry.css('td[data-stat="rush_att"]').text.to_f
hash[:rush_yards] = entry.css('td[data-stat="rush_yds"]').text.to_f
hash[:rush_yards_per_attempt] = entry.css('td[data-stat="rush_yds_per_att"]').text.to_f
hash[:rush_touchdowns] = entry.css('td[data-stat="rush_td"]').text.to_f
elsif(stat_type == "receiving")
hash[:receptions] = entry.css('td[data-stat="rec"]').text.to_f
hash[:receiving_yards] = entry.css('td[data-stat="rec_yds"]').text.to_f
hash[:receiving_yards_per_catch] = entry.css('td[data-stat="rec_yds_per_att"]').text.to_f
hash[:receiving_touchdowns] = entry.css('td[data-stat="rec_td"]').text.to_f
elsif(stat_type == "defense")
hash[:interceptions_caught] = entry.css('td[data-stat="def_int"]').text.to_f
hash[:passes_defended] = entry.css('td[data-stat="pass_defended"]').text.to_f
hash[:tackles] = entry.css('td[data-stat="tackles_combined"]').text.to_f
hash[:sacks] = entry.css('td[data-stat="sacks"]').text.to_f
hash[:tackles_for_loss] = entry.css('td[data-stat="tackles_loss"]').text.to_f
hash[:quarterback_hits] = entry.css('td[data-stat="qn_hits"]').text.to_f
end
out.push(hash)
end
end
return out
end
|