VOOZH about

URL: https://www.geeksforgeeks.org/python/count-the-number-of-paragraph-tag-using-beautifulsoup/

⇱ Count the number of paragraph tag using BeautifulSoup - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Count the number of paragraph tag using BeautifulSoup

Last Updated : 7 Apr, 2021

Sometimes, while extracting data from an HTML webpage, do you want to know how many paragraph tags are used in a given HTML document? Don't worry we will discuss about this in this article.

Syntax:

print(len(soup.find_all("p")))

Approach:

Step 1: First, import the libraries, BeautifulSoup, and os.

from bs4 import BeautifulSoup as bs
import os

Step 2: Now, remove the last segment of the path by entering the name of the Python file in which you are currently working.

base=os.path.dirname(os.path.abspath('#Name of Python file in which you are currently working'))

Step 3: Then, open the HTML file from which you want to read the value.

html=open(os.path.join(base, '#Name of HTML file from which you wish to read value'))

Step 4: Moreover, parse the HTML file in BeautifulSoup.

soup=bs(html, 'html.parser')

Step 5: Next, print a certain line if you want to. 

print("Number of paragraph tags:")

Step 6: Finally, calculate and print the number of paragraph tags in the HTML document.

print(len(soup.find_all("p")))

Implementation: 

Example 1

Let us consider the simple HTML webpage, which has numerous paragraph tags.

For finding the number of paragraph tags in the above HTML webpage, implement the following code.

Output:

👁 Image

Example 2

In the below program, we will find the number of paragraph tags on a particular website.

Output:

👁 Image
Comment