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

2024-04-26 19:08:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python3.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)

但是“elts.count!”0“子句的计算结果始终为true,即使通过print语句,我可以看到tehre没有元素


Tags: no元素数量thatcountmsgresultsimages