0

I have the following web page

</div><a href="https://www.emag.ro/laptop-lenovo-thinkbook-15-iil-cu-procesor-intel-core-i7-1065g7-pana-la-3-90-ghz-15-6-full-hd-16gb-512gb-ssd-intel-iris-plus-graphics-free-dos-mineral-grey-20sm003jrm/pd/DKBK1TMBM/#reviews-section" rel="nofollow" class="star-rating-container js-product-url" data-zone="reviews"><div class="star-rating star-rating-read rated-4.02  star-rating-sm  ">
        <div class="star-rating-inner " style="width: 100%"></div>
    </div><div class="star-rating-text ">

I want to extract the rating from this product. For this product, the rating is defined here.

<div class="star-rating star-rating-read rated-4.02  star-rating-sm  ">

And i cannot extract 4.02.

My code looks like :

rating = container.find_all(class_="star-rating star-rating-read rated")[0].text

I know that the above code is not ok, I was able to extract the price and name of the product but I can't extract the rating :(

2 Answers 2

1

Here is a solution you can try out,

import re

# regex extract the decimal digits from string
extract_ = re.compile(r"\d+.\d+") 

for div in container.find_all("div", attrs={"class": 'star-rating'}):
    for attr in div.attrs['class']:
        ratings_ = extract_.search(attr)

        if ratings_:
            print(ratings_.group())  # 4.02
Sign up to request clarification or add additional context in comments.

Comments

0

Try something like this:

rating = str(container.find_all(class_="star-rating")[0])
rindex = rating.index("rated")
print(rating[rindex+6:rindex+10])

2 Comments

it works but some products has after rating the word "stars" and if the rating in an integer the output will be sth like this 5 st or 4 st, if the rating is a float like 4,02 the output is great
Solved... score=rating[rindex+6:rindex+10].replace(' st', '')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.