I'm trying to learn web-scraping and want to extract the price $46.00-$50.00 from the below HTML blob using python
<div class="organic-gallery-offer-section__price">
<p class="elements-offer-price-normal medium" data-e2e-name="price@@normal" title="$46.00-$50.00"><span class="elements-offer-price-normal__price">$46.00-$50.00</span><span class="elements-offer-price-normal__unit">/ Piece</span></p>
<p class="element-offer-minorder-normal medium" data-e2e-name="minOrder"><span class="element-offer-minorder-normal__value">2 Pieces</span><span class="element-offer-minorder-normal__suffix">(Min Order)</span></p>
</div>]
So far the code I have written which is not working out is
item.findAll("div",{"class":"elements-offer-price-normal medium"})
There are many ways how you can extract the desired text, for example:
from bs4 import BeautifulSoup
txt = '''
<div class="organic-gallery-offer-section__price">
<p class="elements-offer-price-normal medium" data-e2e-name="price@@normal" title="$46.00-$50.00"><span class="elements-offer-price-normal__price">$46.00-$50.00</span><span class="elements-offer-price-normal__unit">/ Piece</span></p>
<p class="element-offer-minorder-normal medium" data-e2e-name="minOrder"><span class="element-offer-minorder-normal__value">2 Pieces</span><span class="element-offer-minorder-normal__suffix">(Min Order)</span></p>
</div>'''
soup = BeautifulSoup(txt, 'html.parser')
print(soup.find('span', class_="elements-offer-price-normal__price").text)
Prints:
$46.00-$50.00
Or: Use CSS selector:
print(soup.select_one('div.organic-gallery-offer-section__price span').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.