-
-
Save tuxfight3r/87dc9b894411c7229b6f7bd405328ef6 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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