我编写了这段代码来从此页面的表格中获取文本。当我将其用于第一列时,它可以正常工作:
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
然而,当我尝试从第二列提取相同的数据时:
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
我做错了什么?
print(row.find_all('td')[1].text)
,print(row.find_all('td')[2].text)
等。 - alecxe