[Tutor] beautifulSoup and .next iteration
Jon Crump
jjcrump at myuw.net
Sun Apr 15 20:41:15 CEST 2007
Daniel,
It was kind of you to respond, and your response was a model of clarity.
You correctly surmised from my awkward framing of the question, that what
I wanted was a list of sibling elements between one named anchor and the
next. My problem was, in part, that I still don't think in terms of
functional programming, thus, the function defs you proposed are very
helpful models for me. I do, however, still need to work out how to make
is_anchor() return true only if the anchor name attribute satisfies a
given regex. Starting with your model, I'm sure I'll be able to figure
this out.
Thanks so much for your time!
Jon
>
> You might find the following definitions helpful:
>
> #############################################################
> def get_siblings_to_next_anchor(anchor):
> """Anchor Tag -> element list
>
> Given an anchor element, returns all the nextSiblings elements up to
> (but not including) the next anchor as a list of either Tags or
> NavigatableStrings."""
>
> elt = anchor.nextSibling
> results = []
> while (elt != None) and (not is_anchor(elt)):
> results.append(elt)
> elt = elt.nextSibling
> return results
>
>
> def is_anchor(elt):
> """element -> boolean
> Returns true if the element is an anchor Tag."""
>
> if isinstance(elt, NavigableString):
> return False
> else:
> return elt.name == 'a'
> #############################################################
More information about the Tutor
mailing list