-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathget_olympics.py
More file actions
83 lines (59 loc) · 2.38 KB
/
get_olympics.py
File metadata and controls
83 lines (59 loc) · 2.38 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
import urllib.request
from bs4 import BeautifulSoup
import csv
f = open('output.csv', 'w')
#get winter olympic data
for i in range(21):
#read in list of winter olympics as a dict
wint = csv.DictReader(open("winterolympics.csv"))
winterolympics = []
for row in wint:
winterolympics.append(row)
#set each i to be the year and location
year = (winterolympics[i]['Year'])
locationcity = (winterolympics[i]['City'])
locationcountry = (winterolympics[i]['Country'])
site = 'http://www.sports-reference.com/olympics/winter/%s/' %str(year)
page = urllib.request.urlopen(site)
soup = BeautifulSoup(page)
for row in soup('table', {'class': 'sortable suppress_all stats_table'})[0].tbody('tr'):
year = str(year)
loccity = str(locationcity)
loccountry = str(locationcountry)
tds = row('td')
rk = tds[0].string
country = tds[1].string
gold = tds[2].string
silver = tds[3].string
bronze = tds[4].string
total = tds[5].string
write_to_file = "Winter," + year + "," + loccity + "," + loccountry + "," + rk + "," + country + "," + gold + "," + silver + "," + bronze + "," + total + '\n'
f.write(write_to_file)
#get summer olympic data
for i in range(28):
#read in list of summer olympics as a dict
summ = csv.DictReader(open("summerolympics.csv"))
summerolympics = []
for row in summ:
summerolympics.append(row)
#set each i to be the year and location
year = (summerolympics[i]['Year'])
locationcity = (summerolympics[i]['City'])
locationcountry = (summerolympics[i]['Country'])
site = 'http://www.sports-reference.com/olympics/summer/%s/' %str(year)
page = urllib.request.urlopen(site)
soup = BeautifulSoup(page)
for row in soup('table', {'class': 'sortable suppress_all stats_table'})[0].tbody('tr'):
year = str(year)
loccity = str(locationcity)
loccountry = str(locationcountry)
tds = row('td')
rk = tds[0].string
country = tds[1].string
gold = tds[2].string
silver = tds[3].string
bronze = tds[4].string
total = tds[5].string
write_to_file = "Summer," + year + "," + loccity + "," + loccountry + "," + rk + "," + country + "," + gold + "," + silver + "," + bronze + "," + total + '\n'
f.write(write_to_file)
f.close()