python - How to count the number of lines of code retrieved using beautiful soup? -


is there function in beautiful soup count number of lines retrieved? or there other way can done?

from bs4 import beautifulsoup import string content = open("webpage.html","r") soup = beautifulsoup(content) divtag = soup.find_all("div", {"class":"classname"}) tag in divtag: ultags = tag.find_all("ul", {"class":"classname"}) tag in ultags:   atags = tag.find_all("a",{"class":"classname"})   tag in atags:    name = tag.find('img')['alt']    print(name) 

if meant number of elements retrieved find_all(), try using len() function :

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

update :

you can change logic select specific elements in 1 go, using css selector. way, getting number of elements retrieved easy calling len() function on return value :

imgtags = soup.select("div.classname ul.classname a.classname img") #print number of <img> retreived : print(len(imgtags))  tag in imgtags:     name = tag['alt']     print(name) 

or can keep logic using multiple loops, , manually keep track number of elements in variable :

counter = 0  divtag = soup.find_all("div", {"class":"classname"}) tag in divtag:   ultags = tag.find_all("ul", {"class":"classname"})   tag in ultags:     atags = tag.find_all("a",{"class":"classname"})     tag in atags:      name = tag.find('img')['alt']      print(name)      #update counter:      counter += 1  print(counter) 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -