, python-goose, html-.
, :
from html5lib import parse
with open('page.html') as f:
doc = parse(f.read(), treebuilder='lxml', namespaceHTMLElements=False)
html = doc.getroot()
body = html.xpath('//body')[0]
def sanitize(element):
"""Retrieve all the text contained in an element as a single line of
text. This must be executed only on blocks that have only inlines
as children
"""
out = ' '.join(element.itertext()).replace('\n', ' ')
out = ' '.join(out.split())
return out
def parse(element):
if element.tag in ['div', 'li', 'a', 'body', 'ul']:
if element.text is None or element.text.isspace():
for child in element.getchildren():
yield from parse(child)
else:
yield sanitize(element)
elif element.tag in ['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']:
yield sanitize(element)
else:
try:
print('> ignored', element.tag)
except:
pass
for e in filter(lambda x: len(x) > 80, parse(body)):
print(e)