Deleting a div with a particular class using BeautifulSoup

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)