我正在使用Python 3.7和BeautifulSoup4。执行findAll时如何查找匹配元素的数量?我有这个
# 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)
但是即使通过print语句,“ elts.count!= 0”子句的计算结果始终为true。
答案 0 :(得分:1)
这很简单:
print (len(elts), 'elements matching')