Reputation: 21
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
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 li
s for Age and Bathroom have a title
, these are being excluded
Upvotes: 2