<jupyter_start><jupyter_text>Exercise: Olympics<jupyter_code>import requests
from bs4 import BeautifulSoup<jupyter_output><empty_output><jupyter_text>- Retrieve a list of Olympic **summer sports** using requests.py and beautifulsoup.py and print them<jupyter_code>www = 'https://www.olympic.org'
r = requests.get(f'{www}/sport')
soup = BeautifulSoup(r.text,'lxml')

for sport in soup.select('ul.countries li'):
    print(sport.text.strip())<jupyter_output><empty_output><jupyter_text>- For each summer sport, retrieve all **men’s** and **women’s events** (one level deeper) and print them<jupyter_code># First try your code for one sport 
r = requests.get('https://www.olympic.org/archery')
s = BeautifulSoup(r.text, 'lxml')

for sp in s.select('section.sport-events ul li'):
    print(sp.text.strip())
# Then loop through all sports
for sport in soup.select('ul.countries li'):
    sp = sport.a['href']
    r = requests.get(f'{www}{sp}')
    s = BeautifulSoup(r.text,'lxml')
    for i in s.select('section.sport-events ul li'):
        print(sport.text.strip(),i.text.strip())<jupyter_output><empty_output><jupyter_text>- Option: retrieve a list of all **cities** where summer Olympic games have been held together with their **year**, number of **athletes** and number of participating **countries**.<jupyter_code># Try your code for one O.G.
r = requests.get('https://www.olympic.org/rio-2016')
s = BeautifulSoup(r.text, 'html.parser')
a = s.select_one('div.frame ul')
a = a.select('li')
date = a[0].findAll(text=True)[3].strip()
city = a[1].findAll(text=True)[4].strip()
athletes = a[2].findAll(text=True)[3].strip()
countries = a[3].findAll(text=True)[3].strip()
print(date, city, athletes, countries)

# Then loop through all O.G.'s
www = 'https://www.olympic.org'
r = requests.get(f'{www}/summer-games')
s = BeautifulSoup(r.text, 'lxml')
g = s.select('section.games-section a')

for i in g:
    sg = i.get('href', '')
    r = requests.get(f'{www}{sg}')
    s = BeautifulSoup(r.text, 'html.parser')
    a = s.select_one('div.frame ul')
    a = a.select('li')
    if len(a) > 3:
        date = a[0].findAll(text=True)[3].strip()
        city = a[1].findAll(text=True)[4].strip()
        athletes = a[2].findAll(text=True)[3].strip()
        countries = a[3].findAll(text=True)[3].strip()
        print(i.text.strip(), date, city, athletes, countries)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1790356394129979, 'fraction_numerical': 0.009224318658280923, 'mean_word_length': 4.708133971291866, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 4, 'lorem ipsum': 0, 'www.': 10, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '202530', 'license': 'CC-BY-4.0', 'n_tokens_mistral': 879, 'n_tokens_neox': 864, 'n_words': 191, 'path': '20190926/Exercises/Olympics.ipynb', 'repo_name': 'SNStatComp/CBSAcademyBD', 'size': '2385'}