0

I want the total no of divs present in html file.

I want the solution in python.

I have tried following code to find divs using class but now i want size of that divs.

from bs4 import BeautifulSoup
import random

HTMLFile = open("/home/earth/sample.html", "r")
file = HTMLFile.read()
print(file)

S = BeautifulSoup(file, 'lxml')
Des = S.body
Attr_Tag = [e.name for e in Des.descendants if e.name is not None]
print(Attr_Tag)

mydivs = S.findAll('div',class_="col")
#mydivs=S.select('.col')

print(mydivs)
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95

2 Answers2

0

You're on the track.

findAll method return's a list so you can simply call the built-in len method to find out the size of the list[div]

Just change the last part to this;

mydivs = S.findAll('div',{'class': "col"})
print(len(mydivs))
Vanitas
  • 92
  • 6
0

Using Selenium to count the number of <div> tags you can use the len() function and you can use either of the following locator strategies:

  • Using CLASS_NAME:

    print(len(driver.find_elements(By.CLASS_NAME, "div")))
    
  • Using CSS_SELECTOR:

    print(len(driver.find_elements(By.CSS_SELECTOR, "div")))
    
  • Using XPATH:

    print(len(driver.find_elements(By.XPATH, "//div")))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352