Print text in second column of table using beautifulsoup

Viewed 3100

I wrote this piece of code to retrieve text from a table from this page. When I used it for the first column it works fine:

from bs4 import BeautifulSoup
import urllib2 #xbmc, xbmcgui, xbmcaddon

url = 'http://racing4everyone.eu/formula-e-201516/'
page = urllib2.urlopen(url)
soup = BeautifulSoup(page.read(), 'html.parser')

for row in soup.findAll('table')[0].tbody.findAll('tr'):
    first_column = row.findAll('th')[0].text
    print first_column

However, when I try to extract the same data from the second column:

for row in soup.findAll('table')[0].tbody.findAll('tr'):
    second_column = row.findAll('th')[1].text
    print second_column

I get an error:

ePrix
Traceback (most recent call last):
  File "addon.py", line 9, in <module>
    second_column = row.findAll('th')[1].text
IndexError: list index out of range

What am I doing wrong?

1 Answers
Related