I want the total no of div
s present in html file.
I want the solution in python.
I have tried following code to find div
s using class but now i want size of that div
s.
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)
CodePudding user response:
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))
CodePudding user response:
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")))