This script returns a prettyfied version of an HTML document.
Why it outputs also the string
at the beginning of the document?
from bs4 import BeautifulSoup, Comment
KEEP_ATTRIBUTES = ['id','class']
for tag in soup.findAll(True):
for key, value in tag.attrs.iteritems():
if key not in KEEP_ATTRIBUTES:
tag.attrs = None
doc = editor.getText()
soup = BeautifulSoup(doc)
clean_soup = _remove_attrs(soup)
[comment.extract() for comment in soup.findAll(text=lambda text:isinstance(text, Comment))]
[item.extract() for item in soup.findAll(text=lambda text:isinstance(text, unicode))]
Ok, I got it, changing last line from "print example()" into "example()" solves, but, why?… Thanks…
because example() doesn't return anything. If you put return 'cheese' on the end of the example function, you'll see it adds cheese to the document instead of none. When you say print example(), what your saying is evaluate example() and print the result.
Ok. My very first steps… :-)
Sign up for the SourceForge newsletter:
You seem to have CSS turned off.
Please don't fill out this field.