Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Last active October 23, 2021 15:11
Show Gist options
  • Save pedrominicz/7129daedbcfa0daed9fc3d266690070a to your computer and use it in GitHub Desktop.
Save pedrominicz/7129daedbcfa0daed9fc3d266690070a to your computer and use it in GitHub Desktop.
Remove a specific tag from an HTML file.
#!/usr/bin/env python3
import bs4
import sys
if len(sys.argv) != 3:
print(f'usage: {sys.argv[0]} <tag> <file>')
sys.exit(1)
with open(sys.argv[2], 'r+') as f:
soup = bs4.BeautifulSoup(f, 'html.parser')
for tag in soup.select(sys.argv[1]):
tag.decompose()
f.seek(0)
f.write(str(soup))
f.truncate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment