-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape_nba_com.py
158 lines (129 loc) · 2.89 KB
/
scrape_nba_com.py
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#Last tested: 14-11-11 edited:14-11-11
import re
import csv
from bs4 import BeautifulSoup
from urllib import request
#City names for teams
SITE = 'nba.com'
URL = 'http://www.nba.com/powerrankings/'
abr_list = ['atl',
'bkn',
'bos',
'cha',
'chi',
'cle',
'dal',
'den',
'det',
'gsw',
'hou',
'ind',
'lac',
'lal',
'mem',
'mia',
'mil',
'min',
'noh',
'nyk',
'okc',
'orl',
'phi',
'phx',
'por',
'sac',
'sas',
'tor',
'uta',
'was']
class ScrapeNba:
def __init__(self,URL=URL):
#Initialize Beautiful Soup
#Pull NBA.com power rankings and parse
self.nba_pr = request.urlopen(URL).read()
#print(self.nba_pr)
self.soup = BeautifulSoup(self.nba_pr)
print('---------------NBA.com Initialized-------------------')
def getSite(self):
return(SITE)
def getUrl(self):
return(URL)
def monthToNum(self,date): #IMPROVEMENT: make this enum type
return{
'Jan' : 1,
'Feb' : 2,
'Mar' : 3,
'Apr' : 4,
'May' : 5,
'Jun' : 6,
'Jul' : 7,
'Aug' : 8,
'Sep' : 9,
'Oct' : 10,
'Nov' : 11,
'Dec' : 12
}[date]
def nameToShortName(self,name):
return{
'Oklahoma City' : 'okc',
'Indiana' : 'ind',
'Houston': 'hou',
'Miami' : 'mia' ,
'L.A. Clippers' : 'lac',
'San Antonio' : 'sas',
'Dallas' : 'dal',
'Phoenix' : 'phx',
'Memphis' : 'mem',
'Portland' : 'por',
'Toronto' : 'tor',
'Golden State' : 'gsw',
'Chicago' : 'chi',
'Washington' : 'was',
'Brooklyn' : 'bkn',
'Atlanta' : 'atl',
'Minnesota' : 'min',
'Charlotte' : 'cha',
'New Orleans' : 'noh',
'Detroit' : 'det',
'New York' : 'nyk',
'Denver' : 'den',
'Utah' : 'uta',
'Orlando' : 'orl',
'Cleveland' : 'cle',
'Boston' : 'bos',
'Sacramento' : 'sac',
'L.A. Lakers' : 'lal',
'Milwaukee' : 'mil',
'Philadelphia' : 'phi'
}[name]
def getDatePublished(self):
self.date_raw = self.soup.find("p", attrs={ "class" : "nbaStoryDatePosted" })
#Pull date and parse
p = re.search('(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\s(\d+)(?:,)\s(20\d+)',self.date_raw.text)
#print(self.date_raw)
#print(p.group(0) + "--")
date_parsed = [ self.monthToNum(p.group(1)), (p.group(2)), (p.group(3)) ]
date = date_parsed[2] + '-' + str(date_parsed[0]).zfill(2) + '-' + str(date_parsed[1]).zfill(2)
return(date)
#Put all data in string
def getRanks(self):
teamList = []
#Initialize Beautiful Soup
#Pull NBA.com power rankings and parse
ratings_raw = self.soup.find_all("div",attrs={ "class" : "nbaArticlePRRight" })
#Create rankings list
i = 1
#IMPROVEMENT: why not create a list with team names and enumerate()
for rank in ratings_raw:
#print(rank.b.text)
teamList.append((self.nameToShortName(rank.b.text),i))
i+=1
teamList.sort()
teamRanks = [ranks for names,ranks in teamList]
"""
#Write to file
f = open('myfile.txt','a')
f.write(out)
f.close()
"""
return(teamRanks)