LiveScore BeautifulSoup Python
Question: I am using BeautifulSoup to parse this site:

<http://www.livescore.com/soccer/champions-league/>

I'm looking to get the links for the rows with numbers:

    
    
    FT  Zenit St. Petersburg    3 - 0   Standard Liege"
    

The 3 - 0 is a link a link; what I want to do is find every link with numbers
(so not results like

    
    
     15:45  APOEL Nicosia   ? - ?   Paris Saint Germain
    

), so I can go load these links and parse out the minute data (`<td
class="min">`)

Hi!!! Needs edited. Now I'm able to get the links. Like this:

    
    
    import urllib2, re, bs4
    
    sitioweb = urllib2.urlopen('http://www.livescore.com/soccer/champions-league/').read()
    soup = bs4.BeautifulSoup(sitioweb)
    href_tags = soup.find_all('a', {'class':"scorelink"})
    
    links = []
    
    for x in xrange(1, len(href_tags)):
        insert = href_tags[x].get("href");links.append(insert)
    
    print links
    

Now my problem is the following: I want to write all this into a DB (like
sqlite) with the number of minute in which a goal was made (this information I
can get from the link I get) but this is possible only in the case that the
goal count is not ? - ?, as there isn't any goal made.

I hope you can understand me...

Best regards and thanks a lot for your help,

Marco


Answer: The following search matches only your links:

    
    
    import re
    
    links = soup.find_all('a', class_='scorelink', href=True,
                          text=re.compile('\d+ - \d+'))
    

The search is limited to:

  * `<a>` tags
  * with the class `scorelink`
  * a non-empty `href` attribute
  * and the link text containing two digits separated by a dash.

Extracting just the links is then trivial:

    
    
    score_urls = [link['href'] for link in soup.find_all(
                      'a', class_='scorelink', href=True, text=re.compile('\d+ - \d+'))]
    

Demo:

    
    
    >>> from bs4 import BeautifulSoup 
    >>> import requests
    >>> from pprint import pprint
    >>> soup = BeautifulSoup(requests.get('http://www.livescore.com/soccer/champions-league/').content)
    >>> [link['href'] for link in soup.find_all('a', class_='scorelink', href=True, text=re.compile('\d+ - \d+'))]
    ['/soccer/champions-league/group-e/cska-moscow-vs-manchester-city/1-1821202/', '/soccer/champions-league/qualifying-round/zenit-st-petersburg-vs-standard-liege/1-1801440/', '/soccer/champions-league/qualifying-round/apoel-nicosia-vs-aab/1-1801432/', '/soccer/champions-league/qualifying-round/bate-borisov-vs-slovan-bratislava/1-1801436/', '/soccer/champions-league/qualifying-round/celtic-vs-maribor/1-1801428/', '/soccer/champions-league/qualifying-round/fc-porto-vs-lille/1-1801444/', '/soccer/champions-league/qualifying-round/arsenal-vs-besiktas/1-1801438/', '/soccer/champions-league/qualifying-round/athletic-bilbao-vs-ssc-napoli/1-1801446/', '/soccer/champions-league/qualifying-round/bayer-leverkusen-vs-fc-koebenhavn/1-1801442/', '/soccer/champions-league/qualifying-round/malmo-ff-vs-salzburg/1-1801430/', '/soccer/champions-league/qualifying-round/pfc-ludogorets-razgrad-vs-steaua-bucuresti/1-1801434/']
    >>> pprint(_)
    ['/soccer/champions-league/group-e/cska-moscow-vs-manchester-city/1-1821202/',
     '/soccer/champions-league/qualifying-round/zenit-st-petersburg-vs-standard-liege/1-1801440/',
     '/soccer/champions-league/qualifying-round/apoel-nicosia-vs-aab/1-1801432/',
     '/soccer/champions-league/qualifying-round/bate-borisov-vs-slovan-bratislava/1-1801436/',
     '/soccer/champions-league/qualifying-round/celtic-vs-maribor/1-1801428/',
     '/soccer/champions-league/qualifying-round/fc-porto-vs-lille/1-1801444/',
     '/soccer/champions-league/qualifying-round/arsenal-vs-besiktas/1-1801438/',
     '/soccer/champions-league/qualifying-round/athletic-bilbao-vs-ssc-napoli/1-1801446/',
     '/soccer/champions-league/qualifying-round/bayer-leverkusen-vs-fc-koebenhavn/1-1801442/',
     '/soccer/champions-league/qualifying-round/malmo-ff-vs-salzburg/1-1801430/',
     '/soccer/champions-league/qualifying-round/pfc-ludogorets-razgrad-vs-steaua-bucuresti/1-1801434/']
    

