如何使用美汤统计检索到的代码行数?
How to count the number of lines of code retrieved using beautiful soup?
beautiful soup有统计检索行数的函数吗?还是有其他方法可以做到这一点?
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)
如果您想要获取 find_all()
检索到的元素数量,请尝试使用 len()
函数:
......
redditAll = soup.find_all("a")
print(len(redditAll))
更新:
您可以一次性将逻辑更改为 select 特定元素,使用 CSS select 或。这样,获取检索到的元素数量就像在 return 值上调用 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)
beautiful soup有统计检索行数的函数吗?还是有其他方法可以做到这一点?
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)
如果您想要获取 find_all()
检索到的元素数量,请尝试使用 len()
函数:
......
redditAll = soup.find_all("a")
print(len(redditAll))
更新:
您可以一次性将逻辑更改为 select 特定元素,使用 CSS select 或。这样,获取检索到的元素数量就像在 return 值上调用 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)