Naeem
Naeem

Reputation: 162

Check if a specific class present in HTML using beautifulsoup Python

I am writing a script and want to check if a 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's returning False
res = html.find('i-stars i-stars--large-3 rating-very-large")) #it's returning NONE

Please guide me how I can resolve this issue?If somehow I get title (title="3.0 star rating") that will also work for me. Screenshot of console HTML enter 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>

Upvotes: 4

Views: 9499

Answers (3)

Tim Tharratt
Tim Tharratt

Reputation: 1310

Was having similar problems getting the exact classes. They can be brought back 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)

Upvotes: 2

宏杰李
宏杰李

Reputation: 12168

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

out:

3 star whatever you want print

Upvotes: 1

lucasnadalutti
lucasnadalutti

Reputation: 5958

has_attr is a method that checks if an element has the attribute that you want. class is an attribute, i-stars i-stars--large-3 rating-very-large is its value.

find expects CSS selectors, not class values. So you should instead use html.find('div.i-stars.i-stars--large-3.rating-very-large'). This is because you are looking for a div with all of these classes.

Upvotes: 2

Related Questions