The idea is to find all dl
elements that has class="method"
and replace them with a p
tag:
import urllib2
from bs4 import BeautifulSoup, Tag
# get the html
url = "https://docs.python.org/3/library/stdtypes.html"
soup = BeautifulSoup(urllib2.urlopen(url))
# replace all `dl` elements with `method` class
for elem in soup('dl', class_='method'):
tag = Tag(name='p')
tag.string = '***removed something here***'
elem.replace_with(tag)
print soup.prettify()
UPD (adapted to the question edit):
dl_elems = soup.find_all(['dl'], attrs={'class': ['class', 'method','function','describe', 'classmethod', 'staticmethod']}) # grab all possible dl-elements
sections = soup.find_all(['div'], attrs={'class': 'section'}) #grab all section-elements
for parent in dl_elems + sections:
for elem in parent.find_all('dl', {'class': 'method'}):
tag = Tag(name='p')
tag.string = '***removed something here***'
elem.replace_with(tag)
print dl_elems + sections