Click here to Skip to main content
14,880,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I use BeautifulSoup (version 4, bs4) and Python My issue is the following: I have a small module, it look for info in a website (Champion league results in Livescore) and then prints the goals information.

This is an example of a line that is printed: CSKA Moscow vs Manchester City Points 2 - 2 In minute: 29' CSKA Moscow vs Manchester City Points 2 - 2 In minute: 38'

Roma vs Bayern Munich Points 1 - 7 In minute: 9' Roma vs Bayern Munich Points 1 - 7 In minute: 23' Roma vs Bayern Munich Points 1 - 7 In minute: 25'

The problem is the following: if you look in the site http://www.livescore.com/soccer/champions-league/ and http://www.livescore.com/soccer/champions-league/qualifying-round/zenit-st-petersburg-vs-standard-liege/1-1801440/

forthe match Zenit St. Petersburg 3 - 0 Standard Liege

you will see that there are many goal attemps in the minutes (minutes's the only info I'm getting as of now) but my module prints everything not only when it's not repeated.

And my other problem is: if you look in the middle of the second link I shared, you will see that there there is the marker (like 1 - 0 2 - 0
3 - 0)

I don't know how to put these two things together... here is my source code:

import urllib2, re
from pprint import pprint
from bs4 import BeautifulSoup

soup = BeautifulSoup(urllib2.urlopen('http://www.livescore.com/soccer/championsleague/').read())

data = []
for match in soup.select('table.league-table tr'):
try:
team1, team2 = match.find_all('td', class_=['fh', 'fa'])
except ValueError: # helps to skip irrelevant rows
continue

score = match.find('a', class_='scorelink').text.strip()
data.append({
'team1': team1.text.strip(),
'team2': team2.text.strip(),
'score': score
})

links = []

new_links = []
links_list = [link['href'] for link in soup.find_all('a', class_='scorelink', href=True)]
#print len(links_list)
puntaje = [link.text for link in soup.find_all('a', class_='scorelink')]

#print len(puntaje)
defensores = [link.text for link in soup.find_all('td', class_='fh')]
#print len(defensores)
#print defensores
atacantes = [link.text for link in soup.find_all('td', class_='fa')]
#print len(atacantes)
for x in xrange(len(links_list)):
texttoedit = links_list[x]
new_links.append('http://www.livescore.com'+texttoedit)
for d in xrange(len(new_links)):
minutesoup = BeautifulSoup(urllib2.urlopen(new_links[d]).read())
#print minutesoup_points.find_all('td', class_='scorelink')
#e = minutesoup_points.find_all('td', class_='scorelink'); print e
for link in minutesoup.find_all('td', class_='min'):
a = link.text
#g = [newlink.text for newlink in minutesoup.find_all('a', class_='scorelink')]
#print g
if len(a) >= 1:
print (defensores[d]+" vs "+atacantes[d]+" Points "+puntaje[d]+" In minute:"+a)
#[newlink.text for newlink in minutesoup.find_all('a', class_='scorelink')]
print new_links
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900