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
CodePudding user response:
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 >
2 633
<span >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'