Use of previous elements function in beautiful soup

This recipe helps to understand the use of previous elements iterator. You can use these iterators to move forward or backward in the document.

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

Requirement - BeautifulSoup (bs4)

Beautiful Soup (bs4) is the web scraping python library for pulling the data from web pages, HTML, and XML documents.  

The .previous_elements() iterator is also quite the similar version of .previous_siblings() function. But You can use these iterators to move forward or backward in the document as it was parsed. 

Explore the Real-World Applications of Recommender Systems

Steps to use this iterator:-

  1. Import necessary modules.
  2. Load an HTML document.
  3. Pass the HTML document into the Beautifulsoup() function.
  4. Use .previous_elements() iterator to print the required elements.

 

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

for element in soup.title.next_elements:
   print(repr(element))

What Users are saying..

profile image

Anand Kumpatla

Sr Data Scientist @ Doubleslash Software Solutions Pvt Ltd
linkedin profile url

ProjectPro is a unique platform and helps many people in the industry to solve real-life problems with a step-by-step walkthrough of projects. A platform with some fantastic resources to gain... Read More

Relevant Projects

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

PyTorch Project to Build a LSTM Text Classification Model
In this PyTorch Project you will learn how to build an LSTM Text Classification model for Classifying the Reviews of an App .

NLP Project for Beginners on Text Processing and Classification
This Project Explains the Basic Text Preprocessing and How to Build a Classification Model in Python

End-to-End ML Model Monitoring using Airflow and Docker
In this MLOps Project, you will learn to build an end to end pipeline to monitor any changes in the predictive power of model or degradation of data.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Mastering A/B Testing: A Practical Guide for Production
In this A/B Testing for Machine Learning Project, you will gain hands-on experience in conducting A/B tests, analyzing statistical significance, and understanding the challenges of building a solution for A/B testing in a production environment.

Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

Ensemble Machine Learning Project - All State Insurance Claims Severity Prediction
In this ensemble machine learning project, we will predict what kind of claims an insurance company will get. This is implemented in python using ensemble machine learning algorithms.

Image Classification Model using Transfer Learning in PyTorch
In this PyTorch Project, you will build an image classification model in PyTorch using the ResNet pre-trained model.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.