Skip to content

Instantly share code, notes, and snippets.

@camilonova
Created March 7, 2018 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camilonova/07bf15a80364acde0e31685e83597a8b to your computer and use it in GitHub Desktop.
Save camilonova/07bf15a80364acde0e31685e83597a8b to your computer and use it in GitHub Desktop.
Wrap html text if does not have a tag
from bs4 import BeautifulSoup
def wrap(to_wrap, wrap_in):
contents = to_wrap.replace_with(wrap_in)
wrap_in.append(contents)
for obj in Post.objects.all():
soup = BeautifulSoup(obj.content, 'html.parser')
for item in soup.children:
if item.name is None:
wrap(item, soup.new_tag("p"))
obj.content = str(soup)
obj.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment