Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix new Sherdog UI + fix wrong opponent/ref/event + change get_text() to .text + fix encoding to UTF8 #6

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 22 additions & 17 deletions sherdog-parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ def set_pro_fights(self):
Sets up range of pro fights for Fighter instance with html code scraped from the site.
:return: None
"""
not_selected = self.soup.find_all('div', class_='module fight_history')
for line in not_selected:
if line.div.h2.get_text() == 'Fight History - Pro':
pro_fights = line
all_sections = self.soup.find_all('section')
for section in all_sections:
if section.find('div', text='FIGHT HISTORY - PRO'):
pro_fights = section
self.pro_range = pro_fights

def set_name(self):
Expand All @@ -104,7 +104,7 @@ def grab_result_data(self):
try:
finder = self.pro_range.find_all('span', class_='final_result')
for result in finder:
results.append(result.get_text())
results.append(result.text)
except AttributeError:
logging.info(f'Attribute Error while grabbing result data for {self.name}, the data might be missing!')
else:
Expand All @@ -119,8 +119,9 @@ def grab_opponents(self):
fighters = []
try:
finder = self.pro_range.find_all('a')
for index in range(0, len(finder), 2):
fighters.append(finder[index].get_text())
for l in finder:
if '/fighter/' in l['href']:
fighters.append(l.text)
except AttributeError:
logging.info(f'Attribute Error while grabbing opponent data for {self.name}, the data might be missing!')
else:
Expand All @@ -135,8 +136,9 @@ def grab_events(self):
events = []
try:
finder = self.pro_range.find_all('a')
for index in range(1, len(finder), 2):
events.append(finder[index].get_text())
for l in finder:
if '/events/' in l['href']:
events.append(l.text)
except AttributeError:
logging.info(f'Attribute Error while grabbing event data for {self.name}, the data might be missing!')
else:
Expand All @@ -151,8 +153,10 @@ def grab_events_date(self):
events_date = []
try:
finder = self.pro_range.find_all('span', class_='sub_line')
for index in range(0, len(finder), 2):
events_date.append(finder[index].get_text())
for l in finder:
t = l.text
if ' / ' in t:
events_date.append(t)
except AttributeError:
logging.info(f'Attribute Error while grabbing event dates for {self.name}, the data might be missing!')
else:
Expand All @@ -166,9 +170,10 @@ def grab_judges(self):
"""
judges = []
try:
finder = self.pro_range.find_all('span', class_='sub_line')
for index in range(1, len(finder), 2):
judges.append(finder[index].get_text())
finder = self.pro_range.find_all('a')
for l in finder:
if '/referee/' in l['href']:
judges.append(l.text)
except AttributeError:
logging.info(f'Attribute Error while grabbing judges data for {self.name}, the data might be missing!')
else:
Expand Down Expand Up @@ -264,7 +269,7 @@ def save_to_csv(self, filename):
:param filename: string with name of the file we want to save data to; file will be created with given name
:return: None
"""
with open(f'{filename}.csv', 'a', newline='', encoding="ISO-8859-1") as csvfile:
with open(f'{filename}.csv', 'a', newline='', encoding="utf8") as csvfile:
writer = csv.writer(csvfile, delimiter=';')
for index in range(len(self.result_data)):
try:
Expand Down Expand Up @@ -403,7 +408,7 @@ def scrape_all_fighters(filename, filetype='csv'):
:return: None
"""
if filetype == 'csv':
headers = ['Fighter', 'Opponent', 'Result', 'Event', 'Event_date', 'Method', 'Referee', 'Round', 'Time']
headers = ['fighter', 'opponent', 'result', 'event', 'date', 'method', 'referee', 'round', 'time']
with open(f'{filename}.csv', 'w', newline='') as csvfile:
init_writer = csv.writer(csvfile, delimiter=',')
init_writer.writerow(headers)
Expand Down Expand Up @@ -584,7 +589,7 @@ def create_fighter_instance(matching):
}

if filetype == 'csv':
headers = ['Fighter', 'Opponent', 'Result', 'Event', 'Event_date', 'Method', 'Referee', 'Round', 'Time']
headers = ['fighter', 'opponent', 'result', 'event', 'date', 'method', 'referee', 'round', 'time']
with open(f'{filename}.csv', 'w', newline='') as csvfile:
init_writer = csv.writer(csvfile, delimiter=';')
init_writer.writerow(headers)
Expand Down
Loading