Reputation: 13
I would like to not extract the value inside the span element.
<p class="price">
US$12.12
<span class="rrp">US$12.24</span>
</p>
This is the code I'm using and it extracts both US$12.12 and US$12.24 inside the span element.
for price in (soup.find_all('p', attrs={'class': 'price'})):
print(price.text)
Upvotes: 1
Views: 643
Reputation: 25048
You can use contents
to just get the first element
from your result:
print(price.contents[0])
from bs4 import BeautifulSoup
soup = BeautifulSoup('''<p class="price">
US$12.12
<span class="rrp">US$12.24</span>
</p>''', "lxml")
for price in (soup.find_all('p', attrs={'class': 'price'})):
print(price.contents[0])
US$12.12
Upvotes: 1