【发布时间】:2015-10-22 04:11:42
【问题描述】:
这里是 html sn-p 1:
<td class="firstleft lineupopt-name" style=""><a href="/link/link_url?id=222" title="Donald Trump" target="_blank">Trump, Donald</a> <span style="color:#666;font-size:10px;">B</span> <span style="color:#cc1100;font-size:10px;font-weight:bold;">TTT</span></td>
这里是 html sn-p 2:
<td class="firstleft lineupopt-name" style=""><a href="/link/link_url2?id=221" title="Hillary Clinton" target="_blank">Clinton, Hillary</a> <span style="color:#cc1100;font-size:10px;font-weight:bold;">TTT</span></td>
这是我的相关代码:
all = cols[1].find_all('span')
for ele in all:
if (ele is not None):
ttt = cols[1].span.text
else:
ttt = 'none'
问题:我的代码在这两种情况下都有效,但对于 html sn-p 2,它从第一个 span 标签中获取内容。在这两种情况下,如果标签存在,我只想从最后一个 span 标签中获取内容。如何才能做到这一点?
【问题讨论】:
标签: python html parsing beautifulsoup html-parsing