Skip to main content
The Cloistered Monkey
  • Pages
  • Archive
  • Tags
  • RSS feed
  • Projects
    Ape Iron Beach Pig Rump & Thigh Bowling For Data Give the Fish Neurotic Networking Terribilis Ludum Visions, Voices, Data
  • Source

Replacing Tags With Beautiful Soup

Cloistered Monkey

2020-08-05 16:53

Source

Notes

To replace a tag in Beautful Soup, find the element then call its replace_with method passing in either a string or tag.

from bs4 import BeautifulSoup
soup = BeautifulSoup(html)

element = soup.find(id=tag_id)
element.replace_with("text")

tag = soup.new_tag("b")
tag.string = "be bold"
element.replace_with(tag)
  • beautiful soup
  • html
  • slipnote
  • web-scraping
  • Previous post
  • Next post
Scribbled by the Cloistered Monkey - Powered by Nikola
This work is licensed under CC BY 4.0