Skip to content Skip to sidebar Skip to footer

Python BeautifulSoup Getting A Column From Table - IndexError List Index Out Of Range

Python newbie here. Python 2.7 with beautifulsoup 4. I am trying to get parse a webpage to get columns using BeautifulSoup. The webpage has tables inside tables; but table 4 is the

Solution 1:

I took a look, first row in the table is actually a header so under the first tr there are some th, this should work:

>>> mytr = soup.findAll('table')[9].findAll('tr')
>>> for i,row in enumerate(mytr):
...     if i:
...         print i,row.findAll('td')[2]

as in most cases of html parsing, consider a more elegant solution like xml and xpath, like:

>>> from lxml import html
>>> print html.parse(url).xpath('//table[@class="yfnc_datamodoutline1"]//td[2]')

Post a Comment for "Python BeautifulSoup Getting A Column From Table - IndexError List Index Out Of Range"