Created
March 7, 2018 23:24
-
-
Save camilonova/07bf15a80364acde0e31685e83597a8b to your computer and use it in GitHub Desktop.
Wrap html text if does not have a tag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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