简体   繁体   中英

Beautiful Soup: Parsing “Span” element

I keep running into walls, but feel like I'm close here.

HTML block being harvested:

        <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 out the "$369.99" value alone (currency class). Here is my logic so far, which captures both '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

Thanks for your help!

You can either go down by the tree and search for the 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM