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

How to scrape and construct price that is in the same div but different sub classes?

I am trying to construct a price out of two different tags (see picture bellow). How do you nest the search so it looks in the div class="price", for span, and sub tags?

enter image description here

How do I get out the numbers from span and sub tags in div class="price"

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:

coldbeetrootsoup=BeautifulSoup(f,'html.parser')

try:
    price = coldbeetrootsoup.find("span",{"class": None}).text.replace('\n',"")
except:
    price = None

try:
    subprice = coldbeetrootsoup.find("sub",{"class": None}).text.replace('\n',"")
except:
    subprice = None

link: https://www.rimi.lt/e-parduotuve/lt/produktai/vaisiai-darzoves-ir-geles/vaisiai-ir-uogos/obuoliai-/fas-liet-obuoliai-ligol-nuraude-anyks-vnt/p/923923

target: price 1.39 EUR

>Solution :

The thing you want is

import re

import requests
from bs4 import BeautifulSoup

html = requests.get(
    "https://www.rimi.lt/e-parduotuve/lt/produktai/vaisiai-darzoves-ir-geles/vaisiai-ir-uogos/obuoliai-/fas-liet-obuoliai-ligol-nuraude-anyks-vnt/p/923923").text


soup = BeautifulSoup(html, features="html.parser")

price_div = soup.find("div", {"class": "price"})
full_part = price_div.find("span").text
cents_part = price_div.find("sup").text
currency = price_div.find("sub").text

currency = re.sub("\s+", "", currency)

print(f"{full_part}.{cents_part} {currency}")  # 1.39 €/vnt.
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