<jupyter_start><jupyter_text>Using BeautifulSoupLet's get the data about Kings XI Punjab's performance in the IPL We need to import the following libraries.<jupyter_code>import requests
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from bs4 import BeautifulSoup

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Define some placeholder variables.<jupyter_code>URL = 'https://en.wikipedia.org/wiki/20{:02d}_Indian_Premier_League'
SEASON = 'IPL-{:02d}'<jupyter_output><empty_output><jupyter_text>Generating urls - this is where we will get our data from.<jupyter_code>for i in range(8,20):
    print(SEASON.format(i - 7), URL.format(i))
df = pd.DataFrame()

for i in range(8,20):
    
    url = URL.format(i)
    season = SEASON.format(i - 7)
    
    response = requests.get(url)
    soup = BeautifulSoup(response.content,'html.parser')
    
    trs = soup.find_all('tr')
    for tr in trs:
        
        tags = tr.find_all('a')
        
        if len(tags) > 0:
            tds = tr.find_all('td')
            try:
                name = tds[0].text.strip()
                played = int(tds[1].text)
                wins = int(tds[2].text)
                
                if len(name) < 10:
                    continue
                name = name.split('(')[0]
                name = name.strip()
                
                row = [name, season, played, wins]
                df = df.append([row])
#                 print(row)
            except:
                continue
df.columns = ['Team','Season','Played','Wins']
df = df.set_index(['Season','Team'])

df
mean_df = pd.DataFrame()

seasons = list(df.index.levels[0])
teams = list(set(df.index.levels[1]))

for i in seasons:    
    mean_df = mean_df.append(pd.Series(df.loc[i].mean(),name=i))
mean_df['Win %'] = mean_df['Wins'] / mean_df['Played'] * 100

team_dfs = {}
for team in teams:
    team_dfs[team] = pd.DataFrame()
    for i in seasons:    
        try:
            team_dfs[team] = team_dfs[team].append(pd.Series(df.loc[i].loc[team],name=i))
        except:
            pass
    team_dfs[team]['Win %'] = team_dfs[team]['Wins'] / team_dfs[team]['Played'] * 100
        



plt.figure(figsize=(12,9))
plt.plot(mean_df['Win %'],'-',label='Average Wins',alpha=0.5,linewidth=10)
# for team in teams:
#     plt.plot(team_dfs[team]['Win %'],label=team)

plt.plot(team_dfs['Kings XI Punjab']['Win %'],'-or',label='KXIP',linewidth=4,markersize=10,alpha=0.75)
plt.plot(team_dfs['Mumbai Indians']['Win %'],'-ob',label='MI',linewidth=4,markersize=10,alpha=0.75)
plt.plot(team_dfs['Chennai Super Kings']['Win %'],'-oy',label='CSK',linewidth=4,markersize=10,alpha=0.75)


x = plt.gca().xaxis
for item in x.get_ticklabels():
    item.set_rotation(45)
plt.subplots_adjust(bottom=0.25)

plt.legend(loc=2)

ax = plt.gca()
ax.set_yticks(np.arange(0,110,10))

ax.set_ylabel('Win Percentage')
ax.set_xlabel('Season')
ax.set_title('Indian Premier League');<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16045652903658947, 'fraction_numerical': 0.02249076871433367, 'mean_word_length': 3.0324763193504736, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '106483', 'license': 'MIT', 'n_tokens_mistral': 1143, 'n_tokens_neox': 1066, 'n_words': 194, 'path': 'solutions/requests_and_beautifulsoup.ipynb', 'repo_name': 'TheBrainGamingSingh/Web-Data-Integration-Lecture-ACM-CSS', 'size': '2979'}