Taking These Tables and Associating Their Appropriate Teams Together
I was recently assisted with getting the scores from a yahoo NHL page that
would print out the teams and their aforementioned scores in a respective
manner. Here is my code:
from bs4 import BeautifulSoup
from urllib.request import urlopen
url = urlopen("http://sports.yahoo.com/nhl/scoreboard?d=2013-01-19")
content = url.read()
soup = BeautifulSoup(content)
def yahooscores():
results = {}
for table in soup.find_all('table', class_='scores'):
for row in table.find_all('tr'):
scores = []
name = None
for cell in row.find_all('td', class_='yspscores'):
link = cell.find('a')
if link:
name = link.text
elif cell.text.isdigit():
scores.append(cell.text)
if name is not None:
results[name] = scores
for name, scores in results.items():
print ('%s: %s' % (name, ', '.join(scores)) + '.')
yahooscores()
Now, first of all: I am associating this stuff in a function because I am
going to have to change the url constantly to get all the values of every
day of the January month.
The issue here is that while I can print the scores and team text fine, I
am trying to accomplish this:
Ottawa: 1, 1, 2.
Winnipeg: 1, 0, 0.
Pittsburgh: 2, 0, 1
Philadelphia: 0, 1, 0.
See, my code doesn't do that. I was in the process of trying to get that
to happen, but what is complicating the process is that the tables are all
under the same class of "scores" and seemingly, I can't find anything
different amongst them.
In a nutshell, associate teams correctly with each other and have a space
in-between for organization.
No comments:
Post a Comment