Use of previous sibling function in beautiful soup

This recipe helps you to understand the use of the previous sibling function. The next sibling is used to find the previous element of the given element.

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

The ".previous_sibling" is provided by the beautiful soup (bs4) python library to find out the next element of the given element.

 

Required Library - BeautifulSoup (bs4)

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

 

The ".previous_sibling" is also used to navigate between tags that are on the same level as the tree.

This function is used to find the previous element of the given element.

Steps to use this function:-

  1. Import necessary modules.
  2. Load an HTML document.
  3. Pass the HTML document into the Beautifulsoup() function.
  4. Find any tag or element in the document or HTML.
  5. Use .previous_sibling iterator to print  the next element. e.g -  soup.a.previous_sibling

 

Links for the more related projects:-

https://www.projectpro.io/projects/data-science-projects/deep-learning-projects
https://www.projectpro.io/projects/data-science-projects/neural-network-projects

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())

.previous_sibling function:-

# a tag does not have any previous tags.
soup.a.previous_sibling

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

Hands-On Approach to Master PyTorch Tensors with Examples
In this deep learning project, you will learn how to perform various operations on the building block of PyTorch : Tensors.

End-to-End Snowflake Healthcare Analytics Project on AWS-1
In this Snowflake Healthcare Analytics Project, you will leverage Snowflake on AWS to predict patient length of stay (LOS) in hospitals. The prediction of LOS can help in efficient resource allocation, lower the risk of staff/visitor infections, and improve overall hospital functioning.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.

Word2Vec and FastText Word Embedding with Gensim in Python
In this NLP Project, you will learn how to use the popular topic modelling library Gensim for implementing two state-of-the-art word embedding methods Word2Vec and FastText models.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

Build a Face Recognition System in Python using FaceNet
In this deep learning project, you will build your own face recognition system in Python using OpenCV and FaceNet by extracting features from an image of a person's face.

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.

Learn to Build a Siamese Neural Network for Image Similarity
In this Deep Learning Project, you will learn how to build a siamese neural network with Keras and Tensorflow for Image Similarity.

Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.