Python Forum
Beautiful Soup - access a rating value in a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Beautiful Soup - access a rating value in a class
#1
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
Reply
#2
(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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  *Beginner* web scraping/Beautiful Soup help 7ken8 2 792 Jan-28-2021, 04:26 PM
Last Post: 7ken8
  Help: Beautiful Soup - Parsing HTML table ironfelix717 2 946 Oct-01-2020, 02:19 PM
Last Post: snippsat
  Beautiful Soup (suddenly) doesn't get full webpage html j.crater 8 2,543 Jul-11-2020, 04:31 PM
Last Post: j.crater
  Requests-HTML vs Beautiful Soup - How to Choose? robin73 0 1,963 Jun-23-2020, 02:53 PM
Last Post: robin73
  looking for direction - scrappy, crawler, beautiful soup Sly_Corn 2 994 Mar-17-2020, 03:17 PM
Last Post: Sly_Corn
  Beautiful soup truncates results jonesjoz 4 1,507 Mar-09-2020, 06:04 PM
Last Post: jonesjoz
  Beautiful soup and tags starter_student 11 2,602 Jul-08-2019, 03:41 PM
Last Post: starter_student
  Beautiful Soup find_all() kirito85 2 1,916 Jun-14-2019, 02:17 AM
Last Post: kirito85
  [split] Using beautiful soup to get html attribute value moski 6 3,540 Jun-03-2019, 04:24 PM
Last Post: moski
  Using beautiful soup to get html attribute value graham23s 2 13,705 Apr-23-2019, 09:21 PM
Last Post: graham23s

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020