Home > Software design >  Extract text from class 'bs4.element.Tag' beautifulsoup
Extract text from class 'bs4.element.Tag' beautifulsoup

Time:11-21

I have the following text in a class 'bs4.element.Tag' object:

<span id="my_rate">264.46013</span>

How do I strip the value of 264.46013 and get rid of the junk before and after the value?

I have seen this and this but I am unable to use the text.split() methods etc.

Cheers

CodePudding user response:

I'm not sure I follow, however, if you are using BeautifulSoup:

from bs4 import BeautifulSoup as bs

html = '<span id="my_rate">264.46013</span>'

soup = bs(html, 'html.parser')
value = soup.select_one('span[id="my_rate"]').get_text()
print(value)

Result:

264.46013
  • Related