from bs4 import BeautifulSoup
import urllib2
import pandas as pd
%matplotlib inline
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import style
pylab.rcParams['figure.figsize'] = (15, 6)
def get_table(cuntry,cup):
if cup == 0:
cup = "FIFA_World_Cup"
else:
cup = "Copa_Am.C3.A9rica"
wiki = "https://en.wikipedia.org/wiki/{}_national_football_team".format(cuntry)
header = {'User-Agent': 'Mozilla/5.0'}
req = urllib2.Request(wiki,headers=header)
page = urllib2.urlopen(req)
soup = BeautifulSoup(page)
foundtext = soup.find('span',id=cup)
table2 = foundtext.findNext('table')
return table2
def get_WC_rank(cuntry,cup):
arg_list = []
table2 = get_table(cuntry,cup)
for row in table2.findAll("tr"):
cells = row.findAll("td")
aelm = row.findAll('a')
arg_dict = {}
try:
rank = str(cells[2].find(text=True))
arg_dict['year'] = int(aelm[1].getText())
arg_dict['host'] = str(row.find('a').get('title') )
arg_dict['rank'] = int(filter(str.isdigit, rank))
arg_list.append(arg_dict)
except:
arg_dict['rank']= 0
df = pd.DataFrame(arg_list)
return df
wc = get_WC_rank('Argentina',0)
wc
ca = get_WC_rank('Chile',0)
ca
br = get_WC_rank('Brazil',0)
br
y = wc['year']
x = wc['rank']
y1 = ca['year']
x1 = ca['rank']
y2 = br['year']
x2 = br['rank']
plt.plot(y,-x,label='arg',marker="o", linestyle="dashed",color="red",linewidth=2)
plt.plot(y1,-x1,label='Chile',marker="o", linestyle="dashed",color="blue",linewidth=2)
plt.plot(y2,-x2,label='brazil',marker="o", linestyle="dashed",color="green",linewidth=2)
style.use('ggplot')
plt.title('World Cup')
plt.ylabel('Rank')
plt.xlabel('year')
#plt.yticks(range(-1,-27))
plt.xticks(range(1930,2016,4))
plt.legend(bbox_to_anchor=(1, 0.5))
#Showing what we plotted
plt.show()