我尝试使用find_previous函数在字符串“半场控球率”之前找到文本,但似乎不起作用。
html代码:
<div class="content">
<table width="800" align="center" cellspacing="0" cellpadding="0">
<tbody><tr>
<th colspan="5" align="center" bgcolor="#1381be">本场技术统计</th>
</tr>
<tr><td width="295" align="right" class="bg1"><div class="barBg2"><div class="info" style="width:44%;"></div></div></td><td width="50" align="center" class="bg1">44%</td><td width="110" align="center" class="bg3">半场控球率</td><td width="50" align="center" class="bg1">56%</td><td width="295" align="left" class="bg1"><div class="barBg"><div class="info" style="width:56%;"></div></div></td></tr>
</tbody></table>
</div>
Python代码:
#coding:utf-8
from bs4 import BeautifulSoup
data = """
<div class="content">
<table width="800" align="center" cellspacing="0" cellpadding="0">
<tbody><tr>
<th colspan="5" align="center" bgcolor="#1381be">本场技>术统计</th>
</tr>
<tr><td width="295" align="right" class="bg1"><div class="barBg2"><div class="info" style="width:44%;"></div></div></td><td width="50" align="center" class="bg1">44%</td><td width="110" align="center" class="bg3">半场控球率</td><td width="50" align="center" class="bg1">56%</td><td width="295" align="left" class="bg1"><div class="barBg"><div class="info" style="width:56%;"></div></div></td></tr>
</tbody></table>
</div>
"""
soup = BeautifulSoup(data, 'html.parser')
print soup.find(string="半场控球率").find_next('td').contents[0]
print soup.find(string="半场控球率").find_previous('td').contents[0]
代码结果:
56%
半场控球率
预期结果:
44%
56%
答案 0 :(得分:1)
尝试一下:
soup.find("td",text="半场控球率").find_next().contents[0]
soup.find("td",text="半场控球率").find_previous().contents[0]
您将得到:
'56%'
'44%'