如何使用BeautifulSoup计算匹配标签的数量?

时间:2019-06-11 21:47:25

标签: python regex python-3.x beautifulsoup match

我正在使用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。

1 个答案:

答案 0 :(得分:1)

这很简单:

print (len(elts), 'elements matching')