在python BS4中提取特定文本?
我试图提取BS4中的某些文本。下面是示例HTML在python BS4中提取特定文本?,python,html,beautifulsoup,Python,Html,Beautifulsoup,我试图提取BS4中的某些文本。下面是示例HTML 2. 如果我理解正确,您希望提取3个分隔的字符串。您可以将.get_text()与自定义的分隔符=字符一起使用,然后在此字符上拆分: from bs4 import BeautifulSoup txt = ''' <tr id="_Gonzaga" class="seedrow"> <td title="Click to show/hide ranks" cla
2.
如果我理解正确,您希望提取3个分隔的字符串。您可以将.get_text()
与自定义的分隔符=
字符一起使用,然后在此字符上拆分:
from bs4 import BeautifulSoup
txt = '''
<tr id="_Gonzaga" class="seedrow">
<td title="Click to show/hide ranks" class='lowrowclick' style="text-align:center;font-size:8px">2</td>
<td id='Gonzaga' class="teamname"><a href="team.php?team=Gonzaga&year=2019" style="text-decoration: none;">Gonzaga<span class="lowrow" style="font-size:10px"><br/> 1 seed, <span style='background-color:#BAE2C6'>Elite Eight</span></span></a></td>
</tr>'''
soup = BeautifulSoup(txt, 'html.parser')
data = soup.findAll('tr', attrs={"class": "seedrow"})
for item in data:
team_name = item.find('td', class_ = 'teamname')
a, b, c = team_name.get_text(strip=True, separator='|').split('|')
print(a)
print(b.strip(','))
print(c)
如果您不喜欢
\xa0
,可以.strip('\xa0')
或.replace('\xa0','')
Gonzaga
1 seed
Elite Eight