Check if any particular class is present in HTML with beautifulsoup Python

I am writing a script and want to check if any particular class is present in html or not.

from bs4 import BeautifulSoup
import requests

def makesoup(u):
    page=requests.get(u)
    html=BeautifulSoup(page.content,"lxml")
    return html
html=makesoup('https://www.yelp.com/biz/soco-urban-lofts-dallas')

print("3 star",html.has_attr("i-stars i-stars--large-3 rating-very-large")) #it returning False
res = html.find('i-stars i-stars--large-3 rating-very-large")) #it returning NONE

Please tell me how can I solve this problem? If anyway, I will get a headline (name = "3.0 star rating") that will also work for me. HTML console screenshotenter image description here

<div class="i-stars i-stars--large-3 rating-very-large" title="3.0 star rating">
  <img class="offscreen" height="303" src="https://s3-media1.fl.yelpcdn.com/assets/srv0/yelp_design_web/8a6fc2d74183/assets/img/stars/stars.png" width="84" alt="3.0 star rating">
    </div>
+4
source share
3 answers

has_attr is a method that checks if an element has an attribute . classis an attribute, i-stars i-stars--large-3 rating-very-largeis its meaning .

find CSS, . html.find('div.i-stars.i-stars--large-3.rating-very-large'). , div .

0
from bs4 import BeautifulSoup
import requests

def makesoup(u):
    page=requests.get(u)
    html=BeautifulSoup(page.content,"lxml")
    return html
html=makesoup('https://www.yelp.com/biz/soco-urban-lofts-dallas')
res = html.find(class_='i-stars i-stars--large-3 rating-very-large')
if res:
    print("3 star", 'whatever you want print')

3 star whatever you want print
0

Had similar problems getting exact classes. They can be returned as a dictionary object as follows.

html = '<div class="i-stars i-stars--large-3 rating-very-large" title="3.0 star rating">'
soup = BeautifulSoup(html, 'html.parser')
find = soup.div
classes = find.attrs['class']
c1 = find.attrs['class'][0]
print (classes, c1)
0
source

Source: https://habr.com/ru/post/1661604/


All Articles