Thursday, December 26, 2024
Google search engine
HomeLanguagesHow to Scrape Nested Tags using BeautifulSoup?

How to Scrape Nested Tags using BeautifulSoup?

We can scrap the Nested tag in beautiful soup with help of. (dot) operator. After creating a soup of the page if we want to navigate nested tag then with the help of. we can do it. For scraping Nested Tag using Beautifulsoup follow the below-mentioned steps.

Step-by-step Approach

Step 1: The first step will be for scraping we need to import beautifulsoup module and get the request of the website we need to import the requests module.

from bs4 import BeautifulSoup
import requests

Step 2: The second step will be to request the URL call get method.

page=requests.get(sample_website)

Step 3: The third step will be for creating soup use beautifulsoup method and for the HTML parse tree use an HTML parser.

BeautifulSoup(page.content, 'html.parser')

Step 4: The fourth step will be to perform .operator till when we want the tag for scrap nested tag, if we want to scrap tag inside body and table then we will use the below statement to scrape nested tags.

soup.body.table.tag

Implementations

Below are various examples that depict how to scrape different nested tags from a particular URL

Example 1:

Python3




from bs4 import BeautifulSoup
import requests
 
# sample website
 
# call get method to request the page
page = requests.get(sample_website)
 
# with the help of BeautifulSoup method and
# html parser created soup
soup = BeautifulSoup(page.content, 'html.parser')
 
# With the help of . operator we will scrap a tag
# under body->ui->i
# here we will go a tag inside body then ul then
# i.means under the body tag we will go to ul tag
# and again inside the ul tag we will go i tag
print(soup.body.ul.i)


Output:

<i class="gfg-icon gfg-icon_arrow-down gfg-icon_header"></i>

Example 2:

Python3




from bs4 import BeautifulSoup
import requests
 
# sample website
 
# call get method to request the page
page = requests.get(sample_website)
 
# with the help of BeautifulSoup method and html
# parser created soup
soup = BeautifulSoup(page.content, 'html.parser')
 
# With the help of . operator we will scrap a tag
# under body->a
# here we will go a tag inside body then a then
# li.means under the body tag we will go to a tag
print(soup.body.a)


Output:

<a class="gfg-stc" href="#main" style="top:0">Skip to content</a>

Example 3:

Python3




from bs4 import BeautifulSoup
import requests
 
# sample website
 
# call get method to request the page
page = requests.get(sample_website)
 
# with the help of BeautifulSoup method and
# html parser created soup
soup = BeautifulSoup(page.content, 'html.parser')
 
#With the help of . operator we will scrap a
# tag under body->a
# here we will go a tag inside body then a then
# li.means under the body tag we will go to a tag 
print(soup.body.a)
 
# With the help of . operator we will scrap a
# tag under body->ui->li
# here we will go a tag inside body then ul then
# li.means under the body tag we will go to ul tag
# and again inside the ul tag we will go li tag
# and inside to li tag we will go to a tag
print(soup.body.ul.li.a)


Output:

<a href=”https://www.geeksforgeeks.org/analysis-of-algorithms-set-1-asymptotic-analysis/” target=”_self”>Asymptotic Analysis</a>

RELATED ARTICLES

Most Popular

Recent Comments