Beautiful Soup: Analysis of the Span Element

I keep bumping into walls, but I feel like I'm near.

Collected HTML block:

        <div class="your-price">
            <span class="label">Your Price</span>
            <span class="currency">$369.99</span>
            <input type="hidden" name="price"  value="$369.99" />
        </div>

      

I would like to parse the value "$ 369.99" (currency class). Here is my logic so far, which grabs the "label" and "currency" content:

r = requests.get(Base_URL)
soup = BeautifulSoup(r.content)

product_price = soup.find("div", {"class": "your-price"})
print product_price.text

      

Thank you for your help!

+3


source to share


1 answer


You can either walk down the tree or find span

with class="currency"

:

print soup.find("div", class_="your-price").find("span", class_="currency").text

      



Or use CSS selectors

(at least shorter and more readable):

print soup.select('div.your-price span.currency')[0].text

      

+3


source







All Articles