Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

BeautifulSoup: Duplicated Class parsing how to overcome

I am finding some difficulties in parsing the HTML with "class" using BeautifulSoup. The idea is to get the price of an item on a website which has the following HTML exposure:

HTML structure

Therefore, I need the ÂŁ920 as a text.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried the following:

url = 'https://www.tiffany.co.uk/jewelry/necklaces-pendants/tiffany-t-t1-circle-pendant-69901190/'

#Open the url to monitor using a new user agent to avoid website blocks you
req = Request(
    url=url,
    headers={'User-Agent': 'Mozilla/5.0'}
)

#Read the HTML code of the url
webpage = urlopen(req, context=ctx).read()
soup = bs4.BeautifulSoup(webpage, "html.parser")

#Define the HTML element we need to screen and find prices
prices = soup.find("span", {"class": "product-description__addtobag_btn_text-static_price-wrapper_price"}).get_text()
print(prices)

And I am getting "[]" as an answer. I believe that since that the product-description__addtobag_btn_text-static_price-wrapper_price I am interested in a sub-part of the same product-description__addtobag_btn_text-static_price-wrapper_price BeautifulSoup takes the first that has no text. I am not sure how to overcome this.

Thank you!

>Solution :

Data are not loaded as html but as json in a script markup:

<script type="application/ld+json">
            {
                "@context": "http://schema.org",
                "@type": "Product",
                "description": "Tiffany T1 designs reinvent our iconic Tiffany T collection with bold profiles and powerful details. Precisely crafted in 18k yellow gold, this large circle pendant features a beveled edge that makes a striking statement. Wear it solo or layer with necklaces in different lengths for a distinctive look.",
                "name": "Tiffany T T1 Circle Pendant",
                "image": "//media.tiffany.com/is/image/Tiffany/EcomItemL2/tiffany-tt1-circle-pendant-69781926_1030892_ED.jpg?&op_usm=1.0,1.0,6.0&$cropN=0.1,0.1,0.8,0.8&defaultImage=NoImageAvailableInternal&",
                "url": "https://www.tiffany.co.uk/jewellery/necklaces%20&%20pendants/tiffany-t-t1-circle-pendant-69901190/",
                "sku": "69901190",
                "category": "Necklaces & Pendants",
                "material": ["Gold"],
                "color": [],
                "itemCondition": "New",
                "brand": "Tiffany & Co.",
                "offers":{
                    "@type": "Offer",
                    "priceCurrency": "GBP",
                    "price": "6675",
                    "url": "https://www.tiffany.co.uk/jewellery/necklaces%20&%20pendants/tiffany-t-t1-circle-pendant-69901190/"
                }
            }
        </script>

So, you can use:

import json

data = json.loads(soup.find_all('script', {'type': 'application/ld+json'})[-1].get_text())
price = int(data['offers']['price'])

Output:

>>> price
6675
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading