I'm trying to scrape a site that has two types of prices, normal price, and discounted prices.
Normal Price HTML:
<p class="plp__price__325EX">
<span aria-label="$29.99">$29.99</span>
</p>
Discounted price HTML:
<p class="plp__price__325EX plp__salePrice__2ExZ2 plp__price__325EX">
$11.99 // trying to get this price
<span class="plp__priceStrikeThrough__2MAlQ plp__price__325EX">$15.99</span>
</p>
Normal price code:
try:
normal_price_element = item.find('p', {'class', 'plp__price__325EX'})
normal_price = normal_price_element.find('span').text
except:
normal_price = ''
Discounted price code:
try:
disc_price = item.find('p', {'class', 'plp__salePrice__2ExZ2'}).text
except:
disc_price = ''
The only difference between these code snippets is the class name. The problem is that the span in the p class contains plp__price__325EX so the normal price code will run when it shouldn't and get the normal price instead of the discounted price.