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
page = requests.get(sample_website)
soup = BeautifulSoup(page.content, 'html.parser' )
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
page = requests.get(sample_website)
soup = BeautifulSoup(page.content, 'html.parser' )
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
page = requests.get(sample_website)
soup = BeautifulSoup(page.content, 'html.parser' )
print (soup.body.a)
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>
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
22 Jul, 2021
Like Article
Save Article