Python Forum
Beautiful Soup - access a rating value in a class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Beautiful Soup - access a rating value in a class (/thread-33331.html)



Beautiful Soup - access a rating value in a class - KatMac - Apr-16-2021

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


RE: Beautiful Soup - access a rating value in a class - snippsat - Apr-16-2021

(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'