Home > OS >  get text from nested span with bs4
get text from nested span with bs4

Time:01-29

I want to take the name "Felix Wong" from this:

<span>Felix Wong <span > 3 drivers</span></span>

im using get_text but it returns full txt: "Felix Wong 3 drivers"

CodePudding user response:

To get the name "Felix Wong" from the text "Felix Wong 3 drivers", you can use the string split() method. You can split the string on the ' ' character and take the first element of the resulting list, which will be the name. For example:`

txt = "Felix Wong  3 drivers"
name = txt.split(' ')[0]
print(name)`

CodePudding user response:

Use contents to get your goal and select by first index:

soup.select_one('span').contents[0]

Example

from bs4 import BeautifulSoup

html = '''<span>Felix Wong <span > 3 drivers</span></span>'''
soup = BeautifulSoup(html)

soup.select_one('span').contents[0].strip()
  • Related