Class: NbaInfo::Scraper

Inherits:
Object
  • Object
show all
Defined in:
lib/nba_info/scraper.rb

Class Method Summary collapse

Class Method Details

.scrape_scheduleObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/nba_info/scraper.rb', line 67

def self.scrape_schedule
  html = HTTParty.get("https://www.cbssports.com/nba/schedule/")
  doc = Nokogiri::HTML(html)
  schedule = []
  games = doc.css('table.TableBase-table tbody tr')
  games.each do |game|
    schedule << {
      away: game.css('td:first-child span.TeamName a').text,
      home: game.css('td:nth-child(2) span.TeamName a').text,
      time: game.css('td:nth-child(3) div.CellGame a').text,
      tv: game.css('td:nth-child(3) div.CellGame div.CellGameTv').text.strip
    }
  end
  schedule
end

.scrape_statsObject



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
# File 'lib/nba_info/scraper.rb', line 24

def self.scrape_stats
  html = HTTParty.get("http://www.espn.com/nba/standings")
  doc = Nokogiri::HTML(html)
  nba = {:east => [], :west => []}
  records = doc.css('div.Table__ScrollerWrapper table tbody tr')
  records.each_with_index do |r, i|
    wins = r.css('td:first-child span').text
    loss = r.css('td:nth-child(2) span').text
    win_pct = r.css('td:nth-child(3) span').text
    record = "#{wins} - #{loss}"
    gb = r.css('td:nth-child(4) span').text
    ppg = r.css('td:nth-child(9) span').text
    opp_ppg = r.css('td:nth-child(10) span').text
    diff = r.css('td:nth-child(11) span').text
    strk = r.css('td:nth-child(12) span').text
    l_ten = r.css('td:last-child span').text
    if i < 15
      nba[:east] << {
        record: record,
        win_pct: win_pct,
        gb: gb,
        ppg: ppg,
        opp_ppg: opp_ppg,
        diff: diff,
        streak: strk,
        l_ten: l_ten
      }
    else
      nba[:west] << {
        record: record,
        win_pct: win_pct,
        gb: gb,
        ppg: ppg,
        opp_ppg: opp_ppg,
        diff: diff,
        streak: strk,
        l_ten: l_ten
      }
    end
  end
  nba
end

.scrape_teamObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nba_info/scraper.rb', line 3

def self.scrape_team
  html = HTTParty.get('http://www.espn.com/nba/standings')
  doc = Nokogiri::HTML(html)
  nba = {:east => [], :west => []}
  names = doc.css('span.hide-mobile a')
  names.each_with_index do |t, i|
    if i < 15
      nba[:east] << {
        name: t.text,
        place: i + 1
    }
    else
      nba[:west] << {
        name: t.text,
        place: i - 14
    }
    end
  end
  nba
end