scraping data using python
Question: Hello I'm new to this,

But I wrote the following script to scrape the following standings
<http://i.stack.imgur.com/98FPr.png>

website: <http://www.bbc.com/sport/football/spanish-la-liga/table>

Im trying to print the position and team name. Team name prints fine, but for
the position I keep getting none. Can anyone help me get over that please?

    
    
        import urllib2
        from bs4 import BeautifulSoup
    
        url = "http://www.bbc.com/sport/football/spanish-la-liga/table"
    
        soup = BeautifulSoup(urllib2.urlopen(url).read())
    
        for row in soup ("table" , {"class" : "table-stats"})[0].tbody("tr"):
            tds = row("td")
    
        print tds[1].string, tds[2].string
    


Answer: You could have easily found out the problem with an interactive Python shell.

The problem is that `tds[1]` (or in full, `soup("table", {"class":"table-
stats"})[0].tbody("tr")[0]("td")[1]`, just to name one) has the form

    
    
    <td class="position"><span class="no-movement">No movement</span> <span class="position-number">1</span></td>
    

And its `.string` attribute (i.e., the `.string` attribute of the `position`
class) is `None`. You can extract the actual number with
`tds[1].contents[2].string`.

Fully corrected script:

    
    
    #!/usr/bin/env python
    import urllib2
    from bs4 import BeautifulSoup
    
    url = "http://www.bbc.com/sport/football/spanish-la-liga/table"
    soup = BeautifulSoup(urllib2.urlopen(url).read())
    for row in soup ("table" , {"class" : "table-stats"})[0].tbody("tr"):
        tds = row("td")
        print tds[1].contents[2].string, tds[2].string
    

Output:

    
    
    1 Barcelona
    2 Real Madrid
    3 Valencia
    4 Atl Madrid
    5 Sevilla
    6 Villarreal
    7 Málaga
    8 Ath Bilbao
    9 Espanyol
    10 Real Sociedad
    11 Celta de Vigo
    12 Rayo Vallecano
    13 Getafe
    14 Eibar
    15 Elche
    16 Almería
    17 Deportivo de La Coruña
    18 Levante
    19 Granada CF
    20 Córdoba
    

