Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting a div with a particlular class using BeautifulSoup

I want to delete the specific div from soup object.
I am using python 2.7 and bs4.

According to documentation we can use div.decompose().

But that would delete all the div. How can I delete a div with specific class?

like image 618
Riken Shah Avatar asked Aug 18 '15 05:08

Riken Shah


People also ask

What is Find () method in BeautifulSoup?

find() method The find method is used for finding out the first tag with the specified name or id and returning an object of type bs4. Example: For instance, consider this simple HTML webpage having different paragraph tags.


2 Answers

Sure, you can just select, find, or find_all the divs of interest in the usual way, and then call decompose() on those divs.

For instance, if you want to remove all divs with class sidebar, you could do that with

# replace with `soup.findAll` if you are using BeautifulSoup3 for div in soup.find_all("div", {'class':'sidebar'}):      div.decompose() 

If you want to remove a div with a specific id, say main-content, you can do that with

soup.find('div', id="main-content").decompose() 
like image 98
lemonhead Avatar answered Sep 20 '22 12:09

lemonhead


This will help you:

from bs4 import BeautifulSoup  markup = '<a>This is not div <div class="1">This is div 1</div><div class="2">This is div 2</div></a>' soup = BeautifulSoup(markup,"html.parser") a_tag = soup  soup.find('div',class_='2').decompose()  print a_tag 

Output:

<a>This is not div <div class="1">This is div 1</div></a> 

Let me know if it helps

like image 42
Vineet Kumar Doshi Avatar answered Sep 21 '22 12:09

Vineet Kumar Doshi