你好,我是相当新的解析与python和beautifulsoup4的HTML表。所有的进展都很顺利,直到我碰到这个奇怪的表格,它在表格中间使用'th'标签,导致我的语法分析结束并抛出'索引超出范围'的错误。我试过搜索和谷歌无济于事。问题是如何在解析表时忽略或剥离这个流氓'th'标签?如何在解析html表时忽略th标签?
这里是我的代码至今:
from mechanize import Browser
from bs4 import BeautifulSoup
mech = Browser()
url = 'https://www.moscone.com/site/do/event/list'
page = mech.open(url)
html = page.read()
soup = BeautifulSoup(html)
table = soup.find('table', { 'id' : 'list' })
for row in table.findAll('tr')[3:]:
col = row.findAll('td')
date = col[0].string
name = col[1].string
location = col[2].string
record = (name, date, location)
final = ','.join(record)
print(final)
这是导致我的错误的HTML的一个小片段
Convention
Mon Dec 01 00:00:00 PST 2014
12/06/14 - 12/09/14
我想上面这流氓'下面的数据th'表示在桌上的新月开始
2014-11-02
Chilly