我想找到一种方法来解析以下信息:
<tr>
<td class="prodSpecAtribute">Rulebook Chapter</td>
<td colspan="5">
<a href="http://cmegroup.com/rulebook/CME/V/450/452/452.pdf" target="_blank" title="CME Chapter 452">CME Chapter 452</a>
</td>
</tr>
<tr>
<td class="prodSpecAtribute" rowspan="2">
Trading Hours
<br>
(All times listed are Central Time)
</td>
<td>OPEN OUTCRY</td>
<td colspan="4">
<div class="font_black Large_div_td">MON-FRI: 7:20 a.m. - 2:00 p.m.</div>
</td>
</tr>
<tr>
<td>CME GLOBEX</td> #PROBLEM HERER -- WANT this and div below to be one row, considered under class <td class="prodSpecAtribute" rowspan="2"> ... Trading Hours...
<td colspan="4">
<div class="font_black Large_div_td">SUN - FRI: 5:00 p.m. - 4:00 p.m. CT</div>
</td>
</tr>
我能够轻松地解析顶部表格中的信息,如下所示:
soup = BeautifulSoup(page)
left_col = soup.findAll('td', attrs={'class' : 'prodSpecAtribute'})
right_col= soup.findAll('td', colspan=['4', '5'])
所以在这个例子中,有 3 行:2 行class "prodSpecAtribute"
至少有一列对应于每个类。但是,最后一行没有 class,所以我需要一种方法来使用最后一个类并在同一个类下定义这个新的,以及第三行的 2 <td>
:CME GLOBEX and SUN - FRI: 5:00 p.m. - 4:00 p.m. CT
combine_column 方法:
def combine_col(right):
num = len(right)
for i in range(0, num):
text_ = ' '.join(right[i].findAll(text=True))
print text_
return text_