Reputation: 19150
I'm using Python 3.7 and BeautifulSoup 4. How do I find the number of matching elements when I do a findAll? I have this
# Verify that we didn't see a no results message
no_images_msg = "No very similar images were found on Reddit."
elts = soup.body.findAll(text=re.compile("^.*" + no_images_msg + ".*$"))
if elts.count != 0:
print("nothing found" + str(elts.count))
print(str(elts))
return json.dumps(results)
but the "elts.count != 0" clause always evaluates to true, even though through the print statement, I can see tehre are no elements.
Upvotes: 0
Views: 86
Reputation: 4482
It's quite straightforward:
print (len(elts), 'elements matching')
Upvotes: 1