Python Forum

Full Version: Trying to extract style attribute with BeautifulSoup
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

In practising some simple web scraping using Requests and BeautifulSoup, I've come across a challenge I haven't tried before and after several attempts that are getting me nowhere, I thought I'd reach out for some direction please.

With this html:
<span class="bv-rating-stars-container">
<span aria-hidden="true" class="bv-rating-stars bv-rating-stars-off"> ★★★★★ </span>
<span aria-hidden="true" class="bv-rating-stars-on bv-rating-stars" style="width: 97%;"> ★★★★★ </span>
</span>

I'm wanting to get the 'star rating' for this product, which is contained in the 'style' tag. So I'm trying to extract the "width: 97%". I was thinking once I'm able to get the "width: 97%", I should be able to use Regex to extract the 97% part (which I think I would be able to do myself), but I'm stuck on being able to extract the "width: 97%" component.

My last attempt was:
rbc = ratingclass.find('span', class_='bv-rating-stars-container')
        rating= rbc.find('span')['style']
and I get an error of:
Error:
[error]Traceback (most recent call last): File "C:\Users\PycharmProjects\test.py", line 95, in <module> rating= rbc.find('span')['style'] File "C:\Users\PycharmProjects\mytest\venv\lib\site-packages\bs4\element.py", line 1519, in __getitem__ return self.attrs[key] KeyError: 'style'
[/error]

Happy to provide more of the code to get to this point if required, I just thought it may be irrelevant to the challenge.

Thank you for your assistance.
After more trying, I resolved it.

rbs = rbc.find('span', class_ = 'bv-rating-stars-on bv-rating-stars')
        rating = rbs['style']
        print(rating)
Output:
width: 97%;
Thank you for reading.