Removing a div using a special class with BeautifulSoup

I want to remove a specific div object from soup . I am using python 2.7 and bs4 .

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

But this will delete the entire div . How can I remove a div with a specific class?

+7
python beautifulsoup
source share
3 answers

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

For example, if you want to remove all sidebar with the sidebar class, you can do this 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 this with

 soup.find('div', id="main-content").decompose() 
+11
source share

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 this helps.

+4
source share
  from BeautifulSoup import BeautifulSoup >>> soup = BeautifulSoup('<body><div>1</div><div class="comment"><strong>2</strong></div></body>') >>> for div in soup.findAll('div', 'comment'): ... div.extract() ... <div class="comment"><strong>2</strong></div> >>> soup <body><div>1</div></body> 
0
source share

All Articles