0

the HTML looks like:

<div class="Title">
        SIZE
        <span class="required-option-group">
                (required)
        </span>
</div>

my code is: name = soup.find('div', {'class':'Title'}).text.strip(). It returns both 'SIZE' and '(required)', but I only want to grab 'SIZE'.

I believe it has a really easy approach, and just wondering if anybody could help me out.

Thanks in advance!

Mr369
  • 384
  • 4
  • 17

1 Answers1

2

You can isolate the tag's direct text using another .find(text=True)

from bs4 import BeautifulSoup
html="""
<div class="Title">
        SIZE
        <span class="required-option-group">
                (required)
        </span>
</div>
"""
soup=BeautifulSoup(html,'html.parser')
print(soup.find('div', {'class':'Title'}).find(text=True).strip())

Output

SIZE
Bitto
  • 7,937
  • 1
  • 16
  • 38