Vlad Vladov
Vlad Vladov

Reputation: 49

How can I print the first text inside a tag

I have a soup object like:

<span class="nowrap">
     2 633
    <span class="currency rub">q</span>
</span>

I did:

price = item.find('span', class_='nowrap')
price_x = price.text.strip()
print(price_x)

Result: 2 633 q.

How can I get without 'q'. Just: 2 633

Upvotes: 4

Views: 101

Answers (1)

S.B
S.B

Reputation: 16476

The reason is .text gives you all the strings inside the tag concatenated. You should use next_element instead:

from bs4 import BeautifulSoup

text = """<span class="nowrap">
     2 633
    <span class="currency rub">q</span>
</span>"""

soup = BeautifulSoup(text, 'html.parser')
price = soup.find('span', class_='nowrap')
next_element = price.next_element
print(repr(next_element.strip()))

output:

'2 633'

Upvotes: 2

Related Questions