The call to find_parent() could be rewritten using next() as a more lightweight way to find the closest (lowest-level) enclosing block element (the rest of the code is unchanged):
====
def my_all_strings (soup, block_elements=True):
strings = []
last_block_container = None
for element in soup.descendants:
# determine if we have entered a new string context or not
if isinstance(element, NavigableString):
if (block_elements is True):
# separate *every* string (current behavior) new_container = True
elif (block_elements):
# must be a list; use block-element semantics try: this_block_container = next(parent for parent in element.parents if parent.name in block_elements) except StopIteration: this_block_container = None new_container = (this_block_container is not last_block_container) last_block_container = this_block_container
else:
# return one big string new_container = False
if new_container or not strings:
# start a new string strings.append("")
The call to find_parent() could be rewritten using next() as a more lightweight way to find the closest (lowest-level) enclosing block element (the rest of the code is unchanged):
==== True): block_container = None
def my_all_strings (soup, block_elements=
strings = []
last_
for element in soup.descendants:
# determine if we have entered a new string context or not
new_container = True
try:
this_ block_container = next(parent for parent in element.parents if parent.name in block_elements)
except StopIteration:
this_ block_container = None
new_container = (this_block_ container is not last_block_ container)
last_ block_container = this_block_ container
new_container = False
if isinstance(element, NavigableString):
if (block_elements is True):
# separate *every* string (current behavior)
elif (block_elements):
# must be a list; use block-element semantics
else:
# return one big string
if new_container or not strings:
strings. append( "")
# start a new string
return strings
====