Python BeautifulSoup Use Text out of tags and store as variable

Viewed 55

I'm currently trying to parse text from a specific web page, which works fine so far. I'm just struggling "getting" the text to work with it further on.

My code looks like this so far:

basename (URL which will be scraped in general)

request_two = requests.get("https://www.billiger.de/shops?shopsearch=" + basename)

def find_tags_from_class(html):

soup = BeautifulSoup(html.content, "html.parser")
div = soup.find("div", class_="svg-rating-stars large blue")
print(div)

This prints out <div class="svg-rating-stars large blue" style="--rating: 100.0"></div> or <div class="svg-rating-stars large blue" style="--rating: 98.3"></div> (depends on the shop(basename) which is going to be checked by the user)

Since this can vary, I want to check if the rating which is parsed in the class "svg-rating-stars large blue" is larger or equal 80. Is there a possibility to do this ? I'm pretty new to python and webscraping and can't find any solution right now.

1 Answers

You can access the div attributes with get. Then you have to convert the string into a float so you can determine if it is GTE 80. Rating is a boolean, True if >= 80, False otherwise.

rating = False
if div.get('style'):
    try:
        rating = float(div.get('style').split()[-1]) >= 80
    except:
        pass
Related