Python Forum

Full Version: Beautiful Soup - access a rating value in a class
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
On the web page I am am trying to scrape, I have come across the following html tag: <p class="star-rating One">

Currently, I am using the following code (which returns a blank line):
rated = b.find(class_="star-rating").get_text().strip()

How do I get the value "One" from this class?

Thank you for your time.

Kate
(Apr-16-2021, 01:09 PM)KatMac Wrote: [ -> ]How do I get the value "One" from this class?
One is part of the attribute name star-rating One so it's one text.
Will find it even if left out One in search,can find attribute name using attrs.
>>> from bs4 import BeautifulSoup
>>> 
>>> html = '<p class="star-rating One">'
>>> soup = BeautifulSoup(html, 'lxml')
>>> find_p = soup.select_one('.star-rating')
>>> find_p
<p class="star-rating One"></p>
>>> find_p.attrs
{'class': ['star-rating', 'One']}
>>> find_p.attrs.get('class')[1]
'One'