Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getting content from CSS3 pseudo element using BeautifulSoup4

I am currently learning web scraping using Python and Beautiful Soup. I am given a task in which the web page is having star rating inside css pseudo element

<span class="bb_rating bble_50">
  ::before
  ::after 
</span>

bble_50::after {
  content: "\e00b\e00b\e00b\e00b\e00b";
}

enter image description here

I want to know how can I get the content from css psuedo element? Need help. Thanks

like image 598
raju Avatar asked Sep 14 '25 20:09

raju


1 Answers

I don't think you should actually go to parsing CSS here. Just map out the class names to ratings:

class_to_rating = {
    "bble_45": 4.5,
    "bble_50": 5
}
elm = soup.select_one(".bb_rating")
rating_class = next(value for value in elm["class"] if value.startswith("bble_"))

print(class_to_rating.get(rating_class, "Unknown rating"))
like image 199
alecxe Avatar answered Sep 16 '25 11:09

alecxe