Skip to content

Instantly share code, notes, and snippets.

@schwartzmx
Last active January 19, 2017 15:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save schwartzmx/0c3e9f907bbbabad6d293af100222eaa to your computer and use it in GitHub Desktop.
Save schwartzmx/0c3e9f907bbbabad6d293af100222eaa to your computer and use it in GitHub Desktop.
Simple script to scrape the default grid Olympic medals table on NBC's site and dump the result as JSON
from bs4 import BeautifulSoup
import requests as r
from json import dumps
class Country:
def __init__(self, country, place, gold, silver, bronze, total_medals):
self.country = country
self.place = place
self.gold = gold
self.silver = silver
self.bronze = bronze
self.total_medals = total_medals
data = r.get('http://www.nbcolympics.com/medals')
doc = BeautifulSoup(data.text, 'html.parser')
grid = doc.find_all("table", attrs={"class": "grid-table"})
standings = []
for row in grid[0].tbody.find_all('tr'):
country = row.a.img.get('alt')
td = row.find_all('td')
place = td[0].div.text
gold = td[2].div.text
silver = td[3].div.text
bronze = td[4].div.text
total_medals = td[5].div.text
standings.append(
Country(country, place, gold, silver, bronze, total_medals)
)
print dumps([s.__dict__ for s in standings])
"""
Example output:
[
{
"total_medals":"103",
"gold":"46",
"country":"United States",
"place":"1",
"silver":"28",
"bronze":"29"
},
{
"total_medals":"88",
"gold":"38",
"country":"China",
"place":"2",
"silver":"29",
"bronze":"21"
},
.
.
.
]
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment