1

how can i get the word Rodger Federer only from the html below

<div class="profile-heading--desktop"><h1><span class="profile-heading__rank">#1 </span>Roger Federer</h1><div class="profile-subheading">Athlete, Tennis</div></div>

am using this code

name = soup.find(class_ = 'profile-heading__rank').get_text()

and am getting #1 ​

1
  • If the code you're working in is Python, it would be worth adding that (and the appropriate version) as a tag. Commented Jul 22, 2020 at 14:19

2 Answers 2

1

Use .next_sibling to get the text next to the <h1>:

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
Sign up to request clarification or add additional context in comments.

Comments

0

A other way is to use .find(text=True, recursive=False) after finding h1:

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')
print(soup.find('h1').find(text=True, recursive=False))

Output:

Roger Federer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.