Python BeautifulSoup Getting a column from table - IndexError List index out of range

烈酒焚心 提交于 2019-12-02 03:33:11

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]')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!