【问题标题】:How do I count the number of matching tags using BeautifulSoup?如何使用 BeautifulSoup 计算匹配标签的数量?
【发布时间】:2019-10-26 08:54:47
【问题描述】:

我正在使用 Python 3.7 和 BeautifulSoup 4。当我执行 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 语句,我也可以看到没有元素。

【问题讨论】:

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


    【解决方案1】:

    这很简单:

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

    【讨论】:

      猜你喜欢
      • 2010-12-14
      • 1970-01-01
      • 2020-11-07
      • 1970-01-01
      • 1970-01-01
      • 2018-11-26
      • 2017-01-31
      • 1970-01-01
      • 2015-04-20
      相关资源
      最近更新 更多