我如何才能得到单词罗杰费德勒只从下面的html
<div class="profile-heading--desktop"><h1><span class="profile-heading__rank">#1 </span>Roger Federer</h1><div class="profile-subheading">Athlete, Tennis</div></div>
我正在使用这个代码
name = soup.find(class_ = 'profile-heading__rank').get_text()
我得到了#1
使用.next_sibling
获取旁边的文本:
from bs4 import BeautifulSoup
html = """
<div class="profile-heading--desktop">
<h1>
<span class="profile-heading__rank">#1 </span>
Roger Federer
</h1>
<div class="profile-subheading">
Athlete, Tennis
</div>
</div>
"""
soup = BeautifulSoup(html, 'html.parser')
name = soup.find(class_='profile-heading__rank').next_sibling
print(name) # --> Roger Federer