Skip to content

MODIFYING HTML - Changing tag content

Python
#!/usr/bin/env python3
"""MODIFYING HTML - Changing tag content"""
from bs4 import BeautifulSoup
html = "<html><body><p>Original text</p></body></html>"
soup = BeautifulSoup(html, 'html.parser')
print("Modifying HTML:")
print(f"  Before: {soup.p.string}")
soup.p.string = "Modified text"
print(f"  After: {soup.p.string}")
soup.p['class'] = 'modified'
print(f"  Added class: {soup.p.get('class')}")
print(f"  Modified HTML: {soup.p}")