Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with Beautiful Soup

You're reading from   Getting Started with Beautiful Soup Learn how to extract information from websites using Beautiful Soup and the Python urllib2 module. This practical, hands-on guide covers everything you need to know to get a head start in website scraping.

Arrow left icon
Product type Paperback
Published in Jan 2014
Publisher Packt
ISBN-13 9781783289554
Length 130 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Vineeth G Nair Vineeth G Nair
Author Profile Icon Vineeth G Nair
Vineeth G Nair
Arrow right icon
View More author details
Toc

Deleting tags from the HTML document


Beautiful Soup also allows for the removal of tags from the document. This is accomplished using the decompose() and extract() methods.

Deleting the producer using decompose()

We have added the new producer, phytoplankton.

We can remove this producer entry by removing the div tags first and then the li tags from the ul tag. We will remove the div tag with class="name" using the decompose() method.

third_producer = soup.find_all("li")[2]
div_name = third_producer.div
div_name.decompose()
print(third_producer.prettify())

#output
<li class_="producerlist">
  <div class_="number">
    10000
  </div>
</li>

In the preceding code, we have used find_all() to find all the li entries and we stored the third producer into the third_producer variable. Then, we found the first div tag and removed it using the decompose() method.

Note

The decompose() method is used to remove the tag from an HTML/XML document. It will remove the tag and its children...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime