1

I would like to get the value 100 from the tag below using python and beautiful soup

<span style="font-size:90%"><b>100</b> <cite style="color:#cc0000"><b>-0.10</b> (0.52%)</cite></span>

The code below gives me the following output

100 -0.10 (0.52%)

How can I extract only the value 100?

Code:

from urllib.request import Request, urlopen
import bs4 
import re

url =  'url.com'
req = Request(url, headers = {'User-Agent': 'Mozilla/5.0'})
page = urlopen(req).read()
soup = bs4.BeautifulSoup(page, 'html.parser')
data = soup.find('span',style=re.compile('font-size:90%'))
value = data.text
0

2 Answers 2

3

You can get the first element of soup.contents:

from bs4 import BeautifulSoup as soup
d = soup(page, 'html.parser').find('span', {'style':'font-size:90%'}).contents[0].text

Output:

'100'
Sign up to request clarification or add additional context in comments.

Comments

3

Just Find the <b> tag it will give you 100.

data = soup.find('span',style=re.compile('font-size:90%'))
value = data.find('b').text

1 Comment

oh! for some reason I missed this part from your original answer. Thank you for pointing it out

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.