In [11]:
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)
In [3]:
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
In [4]:
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
In [4]:
 
    
 
In [18]:
wc = get_WC_rank('Argentina',0)
wc
Out[18]:
host rank year
0 Uruguay 2 1930
1 Italy 9 1934
2 Sweden 13 1958
3 Chile 10 1962
4 England 5 1966
5 Mexico 4 1970
6 West Germany 8 1974
7 Argentina 1 1978
8 Spain 11 1982
9 Mexico 1 1986
10 Italy 2 1990
11 United States 10 1994
12 France 6 1998
13 Germany 6 2006
14 South Africa 5 2010
15 Brazil 2 2014
In [17]:
ca = get_WC_rank('Chile',0)
ca
Out[17]:
host rank year
0 Uruguay 5 1930
1 Brazil 9 1950
2 Chile 3 1962
3 England 13 1966
4 West Germany 11 1974
5 Spain 22 1982
6 France 16 1998
7 South Africa 10 2010
8 Brazil 9 2014
In [7]:
br = get_WC_rank('Brazil',0)
br
Out[7]:
host rank year
0 Uruguay 6 1930
1 Italy 14 1934
2 France 3 1938
3 Brazil 2 1950
4 Switzerland 5 1954
5 Sweden 1 1958
6 Chile 1 1962
7 England 11 1966
8 Mexico 1 1970
9 West Germany 4 1974
10 Argentina 3 1978
11 Spain 5 1982
12 Mexico 5 1986
13 Italy 9 1990
14 United States 1 1994
15 France 2 1998
16 Germany 5 2006
17 South Africa 6 2010
18 Brazil 4 2014
19 Russia 6 2018
In [24]:
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()
    
In [230]:
 
In [176]:
 
In [156]:
 
In [156]:
 
    
In [156]:
 
    
In []: