0

I'm trying to extract a number from below HTML code, however I'm recieving the whole class or further rows starting with <div every time I try to use .find function.

My code:

ratescointeiner = checkinscontainer.find('div',class_='caps')

HTML chunk:

<div class="caps" data-rating="2">
<div class="cap cap-100"></div>
<div class="cap cap-100"></div>
<div class="cap"></div>
<div class="cap"></div>
<div class="cap"></div>
</div>

is there a possibility to extract the value of this data-rating attribute only?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • Did you read e.g. https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes? – jonrsharpe Jul 14 '21 at 12:43
  • You are asking it for the div with class = caps. That div starts on your top row and ends on you bottom row, and it has several divs nested inside it. So you are receiving what you asked for. That said, have you tried to get the data-rating attribute for the div you got back? Like ratescointeiner["data-rating"]? – Jeremy Kahan Jul 14 '21 at 12:54

2 Answers2

2

You can use get method to find attribute of associate tag

html="""<div class="caps" data-rating="2">
<div class="cap cap-100"></div>
<div class="cap cap-100"></div>
<div class="cap"></div>
<div class="cap"></div>
<div class="cap"></div>
</div>"""
soup=BeautifulSoup(html,"html.parser")
ratescointeiner = soup.find('div',class_='caps').get("data-rating")
ratescointeiner

Output:

'2'
Bhavya Parikh
  • 3,304
  • 2
  • 9
  • 19
0

i think below answer will help you to resolve your issue Extracting an attribute value with beautifulsoup

Thanks

Parth Shah
  • 128
  • 1
  • 9