-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetfighterlist
178 lines (152 loc) · 6.5 KB
/
getfighterlist
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# app/ program that pulls up info on mma fighters
# enter in name to pull up fighter, or weight class for list of fighters
# sorts by gender
# sorts by belt holders
# sorts by w/l ratio
# import bs4 as bs
import urllib.request
from html_table_parser.parser import HTMLTableParser as ht
import pandas as pd
def url_get_contents(url):
""" Opens a website and read its binary contents (HTTP Response Body) """
req = urllib.request.Request(url=url)
f = urllib.request.urlopen(req)
return f.read()
def get_info():
#after reading and decoding html, parses tables
#checks for specific table (weight class)
#returns row from table if valid input
url = "https://en.wikipedia.org/wiki/List_of_current_UFC_fighters"
xhtml = url_get_contents(url).decode('utf-8')
p = ht()
p.feed(xhtml)
n = False
x = False
while n is False:
print("Weight class values are\n 1-Heavy\n 2-Light heavy\n 3-Middle\n 4-Welter\n 5-Light\n 6-Feather\n 7-Bantam\n 8-Fly\n 9-Female feather\n 10-Female bantam\n 11-Female fly\n 12-Female straw\n")
weight_class = input('\nEnter weight class: ')
if weight_class == 'Heavyweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[9])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Light heavyweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[10])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Middleweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[11])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Welterweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[12])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Lightweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[13])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Featherweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[14])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Bantamweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[15])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Flyweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[16])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Female featherweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[17])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Female bantamweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[18])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Female flyweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[19])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
elif weight_class == 'Female strawweight':
while x is False:
fighter = input('\nEnter fighter name: ')
df = pd.DataFrame(p.tables[20])
if fighter in df[1].values:
result = df.loc[df[1] == fighter]
return result
else:
print('\nEnter valid fighter name (First Last)')
else:
print("\nEnter valid weightclass (<class>weight)\n")
print(get_info())
# df = pd.DataFrame(p.tables[9])
# df = df.drop(col, axis = 1)
# pprint(p.tables[9])
# print(pd.DataFrame(p.tables[9]))
# page = requests.get(url)
# doc = lh.fromstring(page.content)
# tr_elements = doc.xpath('//tr')
# col = []
# i = 0
# for t in tr_elements[1]:
# i += 1
# name = t.text_content()
# print('%d:"%s"'%(i, name))
# col.append((name, []))