Home > database >  Getting a specific piece of text from a beautifulsoup result Python 3
Getting a specific piece of text from a beautifulsoup result Python 3

Time:01-21

I am trying to get this number '96.40' from the following result

<div style="text-decoration: underline">


                                                                    96.40% 
                                                                </div>

This is my code:

result = s.get('example.com')
soup = BeautifulSoup(result.text, "html.parser")
myData = soup.find("div", {"style": "text-decoration: underline"})
print(myData)

CodePudding user response:

You need to print

myData.text.strip()

CodePudding user response:

To get the "text" from a tag you can use .text or .get_text():

myData.text.strip()

or

myData.get_text(strip=True)

In addition you can replace the %:

myData.get_text(strip=True).replace('%','')

or by slice

myData.get_text(strip=True)[:-1]

Example

html = '''
<div style="text-decoration: underline">


                                                                    96.40% 
                                                                </div>
'''
soup = BeautifulSoup(html, "html.parser")
myData = soup.find("div", {"style": "text-decoration: underline"})
print(myData.get_text(strip=True).replace('%',''))

Output

96.40

  •  Tags:  
  • Related