Skip to content

Instantly share code, notes, and snippets.

@leonardreidy
Created June 3, 2014 13:26
Show Gist options
  • Save leonardreidy/40381da2588126928058 to your computer and use it in GitHub Desktop.
Save leonardreidy/40381da2588126928058 to your computer and use it in GitHub Desktop.
How to extract or remove elements from BeautifulSoup soup
# extract (remove) some element from the soup
[s.extract() for s in soup(x)]
# examples
# extract style elements
[s.extract() for s in soup('style')]
# extract script elements
[s.extract() for s in soup('script')]
# retain extracted element
extracted_element = [s.extract() for s in soup(x)]
# remove entire attributes - this seems to work more or less
# equivalently to the previous approach, as far as I can tell
# - check the docs to be sure
for tag in soup():
for attribute in [x1, x2, x3, xn]:
del tag[attribute]
# example
for tag in soup():
for attribute in ['style','id','class']:
del tag[attribute]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment