-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcricket_rankings.py
53 lines (36 loc) · 1.73 KB
/
cricket_rankings.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from bs4 import BeautifulSoup
import urllib2
import prettytable
# Gets the cricket team rankings for men on the basis of the format
def getRankings(format):
url = "https://www.icc-cricket.com/rankings/mens/team-rankings/"+format
file = urllib2.urlopen(url)
rankings = file.read()
file.close()
soup = BeautifulSoup(rankings,'html.parser')
positions = soup.findAll('td',attrs={'class':'table-body__cell table-body__cell--position'})
teams = soup.findAll('td',attrs={'class':'table-body__cell rankings-table__team u-text-left'})
matchesAndPoints = soup.findAll('td',attrs={'class':'table-body__cell'})
ratings = soup.findAll('td',attrs={'class':'table-body__cell u-text-right rating'})
indexForMatches = 2
incrementForMatches = 5
indexForPoints = 3
incrementForPoints = 5
teamDetails = prettytable.PrettyTable(['Position','Team','Matches','Points','Ratings'])
for index in range(len(positions)):
position = positions[index].text.strip(' \t\n\r')
team = teams[index].text.strip(' \t\n\r')
matches = matchesAndPoints[indexForMatches].text.strip(' \t\n\r')
points = matchesAndPoints[indexForPoints].text.strip(' \t\n\r')
rating = ratings[index].text.strip(' \t\n\r')
# print position,team,' ',matches,' ',points,' ',rating
team = [position, team, matches, points, rating]
teamDetails.add_row(team)
indexForMatches += incrementForMatches
indexForPoints += incrementForPoints
print teamDetails
return teamDetails
# Gets the format from the user for which rankings have to be displayed
format = raw_input('Enter format :')
# Invokes getRankings to get the cricket ranking details
teamDetails = getRankings(format)