Use of parents function in beautiful soup

This recipe explains the use of parent function in beautiful soup. The parent function is used to iterate over all the element's parents.

Recipe Objective - Use of ".parents" function in beautiful soup?

The ".parents" function is provided by a beautiful soup python web scraping library to iterate over the element's parents.

 

Required Library - Beautiful Soup (bs4)

Beautiful Soup (bs4) is a Python web scraping library for pulling the data from HTML and XML files.

 

We can iterate over all of the elements with the use of the ".parents" function.

Steps to use this function:-

  1. Import necessary modules.
  2. Load an HTML document.
  3. Pass the HTML document into the Beautifulsoup() function.
  4. Get the tag or element in the document or HTML.
  5. Use the ".parents" function to find out the parents of the tag.

    e.g - for parent in link.parents:
                      print(parent.name) 

              Build a Multi Touch Attribution Model in Python with Source Code 

Example:-

import requests
from bs4 import BeautifulSoup as bs

# load the projectpro webpage content
r = requests.get('https://www.projectpro.io/')

# convert to beautiful soup
soup = bs(r.content)

# printing our web page
print(soup.prettify())

# getting links using tag
links = soup.a
links

.parents function:-

for parent in link.parents:
   print(parent.name)

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Build a Churn Prediction Model using Ensemble Learning
Learn how to build ensemble machine learning models like Random Forest, Adaboost, and Gradient Boosting for Customer Churn Prediction using Python

AWS MLOps Project to Deploy Multiple Linear Regression Model
Build and Deploy a Multiple Linear Regression Model in Python on AWS

Create Your First Chatbot with RASA NLU Model and Python
Learn the basic aspects of chatbot development and open source conversational AI RASA to create a simple AI powered chatbot on your own.

Learn to Build Generative Models Using PyTorch Autoencoders
In this deep learning project, you will learn how to build a Generative Model using Autoencoders in PyTorch

Stock Price Prediction Project using LSTM and RNN
Learn how to predict stock prices using RNN and LSTM models. Understand deep learning concepts and apply them to real-world financial data for accurate forecasting.

NLP Project to Build a Resume Parser in Python using Spacy
Use the popular Spacy NLP python library for OCR and text classification to build a Resume Parser in Python.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

MLOps Project to Build Search Relevancy Algorithm with SBERT
In this MLOps SBERT project you will learn to build and deploy an accurate and scalable search algorithm on AWS using SBERT and ANNOY to enhance search relevancy in news articles.

End-to-End Speech Emotion Recognition Project using ANN
Speech Emotion Recognition using RAVDESS Audio Dataset - Build an Artificial Neural Network Model to Classify Audio Data into various Emotions like Sad, Happy, Angry, and Neutral

Avocado Machine Learning Project Python for Price Prediction
In this ML Project, you will use the Avocado dataset to build a machine learning model to predict the average price of avocado which is continuous in nature based on region and varieties of avocado.