Home > Mobile >  couldn't scrap the price out of a HTML code
couldn't scrap the price out of a HTML code

Time:03-20

I have the HTML code below:

<span >
    <span >
        <bdi>
             <span >R</span>
             1 579
             <sup>00</sup>
        </bdi>
    </span>
</span>

I need to extract the price from there using python in format "1579.00" as a float. how can I do that?

CodePudding user response:

To get the price amount as float you can use next example:

import re
from bs4 import BeautifulSoup

html_doc = """<span >
    <span >
        <bdi>
             <span >R</span>
             1 579
             <sup>00</sup>
        </bdi>
    </span>
</span>"""

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

price = soup.select_one(".amount").text
price = float("".join(re.findall(r"\d ", price))) / 100
print(price)

Prints:

1579.0

Or:

soup.select_one(".woocommerce-Price-currencySymbol").extract()
price = float(
    soup.select_one(".amount")
    .get_text(strip=True, separator=".")
    .replace(" ", "")
)
print(price)

Prints:

1579.0
  • Related