Raphael Ramirez
Raphael Ramirez

Reputation: 33

How to extract discounted prices in HTML using BeautifulSoup

I am trying to practice and learn BeautifulSoup so I tried web parsing an online shop. However, I am having trouble to extract certain prices of some as they have two prices in the HTML - both original price and discounted price. To add to the problem, they are under the same div class and span class.

Full HTML link is here: https://www.parkoutlet.com.ph/collections/mens-footwear?page=1

You can scroll for other page if current page has no SOLD OUT or DISCOUNTED PRICE.

HTML Code Here

This is my current code:

import urllib.request, urllib.parse, urllib.error
from bs4 import BeautifulSoup

url = "https://www.parkoutlet.com.ph/collections/mens-footwear"

# Opening the URL
html = urllib.request.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")

itemContainers = soup.findAll("div", {"class":"grid__item small--one-half medium-up--one-fifth"}) # Finds all the "Shoe Containers"

for container in itemContainers:
    brandContainer = container.findAll("div", class_="product-card__brand") # Brand Name
    brand = brandContainer[0].text

    nameContainer = container.findAll("div", class_="product-card__name") # Product Name
    name = nameContainer[0].text

    try:
        priceContainer = container.findAll("s", class_="product-card__regular-price") # Regular Price
        priceAvail = priceContainer[0].text
        print(priceAvail)

    except:
        noStock = container.findAll("div", class_="product-card__availability") # Sold Out
        print("SOLD OUT")

    print(brand)
    print(name)
    print("")

It gives me

₱2,495
NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. TURF (YOUTH)

₱2,495
NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. MG (YOUTH)

₱2,495
NIKE
NIKE JR. PHANTOM VISION 2 CLUB DYNAMIC FIT FG/MG (YOUTH)

SOLD OUT
NIKE
NIKE MERCURIAL VAPOR 13 CLUB NEYMAR JR. TURF

₱4,300
ADIDAS
ADIDAS X 19 3 FIRM GROUND BOOTS

SOLD OUT
NIKE
NIKE PHANTOM VISION 2 ACADEMY DYNAMIC FIT IC

SOLD OUT
NIKE
NIKE DAY BREAK MENS SHOE

₱5,795
NIKE
PG 4 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
PG 4 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
PG4 EP MENS BASKETBALL SHOE

₱5,795
NIKE
PG4 EP MENS BASKETBALL SHOE

₱5,795
NIKE
PG 4 PCG EP BASKETBALL MENS SHOE

₱5,795
NIKE
PG 4 PCG EP BASKETBALL MENS SHOE

₱6,445
NIKE
NIKE ZOOM FREAK 2

SOLD OUT
NIKE
NIKE AIR PRESTO MENS SHOE

SOLD OUT
NIKE
JORDAN DELTA MENS SHOE

SOLD OUT
NIKE
JORDAN DELTA MENS SHOE

SOLD OUT
NIKE
KYBRID S2 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
KYBRID S2 EP BASKETBALL MENS SHOE

SOLD OUT
NIKE
NIKE AIR MAX 2090 MENS SHOE

Yes. Brand names and Item names are all correct but price isn't. For example, the first item's price is supposed to be P749 because that is the discounted price.

I'm currently a beginner and if there is anything wrong you see in my code besides being unable to get the discounted price, please let me know!

1.) how do i get the discounted price while the original price is at the same div and class

2.) how can i make my code more efficient? is there a better way to approach this project?

Upvotes: 3

Views: 561

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195438

You can use this example how to get normal price/sale price/availability:

import requests
from bs4 import BeautifulSoup

url = "https://www.parkoutlet.com.ph/collections/mens-footwear?page=1"
soup = BeautifulSoup(requests.get(url).content, "html.parser")

for item in soup.select(".grid__item.medium-up--one-fifth"):
    brand = item.select_one(".product-card__brand")
    name = item.select_one(".product-card__name")

    sale_price = ""
    normal_price = item.select_one(".product-card__regular-price")
    if normal_price:
        sale_price = normal_price.find_next().find_next_sibling(text=True)
    else:
        normal_price = item.select_one(".product-card__availability")

    print(brand.get_text(strip=True))
    print(name.get_text(strip=True))
    print(normal_price.get_text(strip=True))
    print(sale_price.strip())
    print("-" * 80)

Prints:

NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. TURF (YOUTH)
₱2,495
₱749
--------------------------------------------------------------------------------
NIKE
NIKE JR. MERCURIAL VAPOR 13 CLUB NEYMAR JR. MG (YOUTH)
₱2,495
₱749
--------------------------------------------------------------------------------
NIKE
NIKE JR. PHANTOM VISION 2 CLUB DYNAMIC FIT FG/MG (YOUTH)
₱2,495
₱749
--------------------------------------------------------------------------------

...

Upvotes: 2

Related Questions