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?
How do I get out the numbers from span and sub tags in div class="price"
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
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.
