1

Trying to process a very simple html5 script and render it using html5lib

import html5lib
html = '''<!DOCTYPE html>
<html lang="en">
 <head>
 <title>Hi</title>
 </head>
 <body>
 <script src="a.js"></script>
 <script src="b.js"></script>
 </body>
</html>
'''
parser = html5lib.HTMLParser(tree = html5lib.treebuilders.getTreeBuilder("lxml"))
walker = html5lib.treewalkers.getTreeWalker("lxml")
serializer = html5lib.serializer.htmlserializer.HTMLSerializer()
document = parser.parse(html)
stream = walker(document)
theHTML = serializer.render(stream)
print theHTML

The output looks like:

<!DOCTYPE html><html lang=en><head>
 <title>Hi</title>
 </head>
 <body>
 <script src=a.js></script>
 <script src=b.js></script>

Yup. It just cuts off mid way. Changing the tree builder from lxml to dom does nothing. Tweaking the HTML changes the output but it's still pretty corrupt.

asked Feb 2, 2012 at 5:35

1 Answer 1

1

So the key seems to be omit_optional_tags=False somehow with that missing it eats the end of the output.

parser = html5lib.HTMLParser(tree = html5lib.treebuilders.getTreeBuilder("lxml"))
document = parser.parse(html) 
walker = html5lib.treewalkers.getTreeWalker("lxml")
stream = walker(document)
s = serializer.htmlserializer.HTMLSerializer(omit_optional_tags=False)
output_generator = s.serialize(stream)
for item in output_generator:
 print item
<!DOCTYPE html>
<html lang=en>
<head>
<title>
Hi
</title>
</head>
<body>
<script src=a.js>
</script>
<script src=b.js>
</script>
</body>
</html>
>>>
answered Feb 2, 2012 at 5:51
Sign up to request clarification or add additional context in comments.

2 Comments

Well unable to reproduce using your code. 's' isn't even defined in your code. Want to edit your response with code that doesn't error?
Ah. So the key seems to be omit_optional_tags=False -- somehow with that missing it eats the end of the output.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.