Moon
Moon

Reputation: 21

How to get <span> text in class with same name in python?

enter image description here

Hello everyone, New Python Learner Here Needs Help on BeautifulSoup

I want to print "Resale" text from "li","class":"keypoint" from the snippet shown in above link and print none where it is not there. I am able to extract old and Bathrooms using the code below in a for loop in python

try:
    print("Age :", item.find("li", {'class': "keypoint", "title": "old"}).text)
except:
    print("Age :", "Not Mentioned")
try:
    print("Bathrooms :", item.find("li", {'class': "keypoint", "title": "Bathrooms"}).text)
except:
    print("Bathrooms :", "Not Mentioned")

Can someone please tell me how do I get "Resale" text from span?

Upvotes: 1

Views: 112

Answers (1)

Wondercricket
Wondercricket

Reputation: 7872

Based on your sample HTML, this should do the trick:

item.find("li", {'class': 'keypoint', 'title': False})

By doing {'title': False}, this searches for any tag where the attribute is not present. Since the lis for Age and Bathroom have a title, these are being excluded

Upvotes: 2

Related Questions