Darkstar
Darkstar

Reputation: 13

BeautifulSoup: How to ignore Nested elements

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

Answers (1)

HedgeHog
HedgeHog

Reputation: 25048

You can use contents to just get the first element from your result:

print(price.contents[0])

Example

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])

Output

  US$12.12

Upvotes: 1

Related Questions