如何计算使用美丽的汤检索的代码行数?

时间:2015-05-15 05:57:24

标签: python printing count beautifulsoup

美丽汤中是否有任何功能可以计算检索到的行数?或者还有其他方法可以做到吗?

from bs4 import BeautifulSoup
import string
content = open("webpage.html","r")
soup = BeautifulSoup(content)
divTag = soup.find_all("div", {"class":"classname"})
for tag in divTag:
ulTags = tag.find_all("ul", {"class":"classname"})
for tag in ulTags:
  aTags = tag.find_all("a",{"class":"classname"})
  for tag in aTags:
   name = tag.find('img')['alt']
   print(name)

1 个答案:

答案 0 :(得分:1)

如果您打算获取find_all()检索到的元素数量,请尝试使用len()函数:

......
redditAll = soup.find_all("a")
print(len(redditAll))

更新:

您可以使用CSS选择器更改逻辑以一次选择特定元素。这样,获取检索的元素数就像在返回值上调用len()函数一样简单:

imgTags = soup.select("div.classname ul.classname a.classname img")
#print number of <img> retreived :
print(len(imgTags))

for tag in imgTags:
    name = tag['alt']
    print(name)

或者您可以使用多个for循环来保持逻辑,并手动跟踪变量中元素的数量:

counter = 0

divTag = soup.find_all("div", {"class":"classname"})
for tag in divTag:
  ulTags = tag.find_all("ul", {"class":"classname"})
  for tag in ulTags:
    aTags = tag.find_all("a",{"class":"classname"})
    for tag in aTags:
     name = tag.find('img')['alt']
     print(name)
     #update counter:
     counter += 1

print(counter)