-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyql_result_parse.py
42 lines (32 loc) · 918 Bytes
/
yql_result_parse.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
from bs4 import BeautifulSoup
import json
category_lookup = {'9004003': 'FGs',
'5': 'FGP',
'9007006': 'FTs',
'8': 'FTP',
'10': '3pt',
'12': 'Pts',
'15': 'Rbs',
'16': 'Ast',
'17': 'Stl',
'18': 'Blk',
'19': 'TOs'}
with open('result_test.txt', 'rb') as f:
xmldata = f.read()
soup = BeautifulSoup(xmldata)
# rename 'name' tags to not confuse .name method
for name_tag in soup.find_all('name'):
name_tag.name = 'name_'
result_array = []
teams = soup.league.scoreboard.matchups.find_all('team')
for each in teams:
team_dict = {}
team_dict['name'] = each.name_.string
for st in each.find_all('stat'):
if category_lookup[st.stat_id.string] in ['FTs', 'FGs']:
team_dict[category_lookup[st.stat_id.string]] = st.value.string
else:
team_dict[category_lookup[st.stat_id.string]] = float(st.value.string)
result_array.append(team_dict)
json_output = json.dumps(result_array)
print json_output