get_parents will find the parent of nested nodes, but sections aren't one of the nodes.
When I'm concerned with the section, I usually iterate over them like below. You could also iterate all nodes in reverse from the node of interest until you find a Heading node. >>> for section in wikicode.get_sections(flat=True, include_lead=False): ... heading = section.filter_headings()[0].title.strip() ... for tpl in section.ifilter_templates(): ... if tpl.name.matches("Template:Foo"): ... this = tpl ... >>> this '{{Template:Foo}}' >>> heading 'foo' On Thu, Mar 23, 2023 at 9:03 AM Roy Smith <r...@panix.com> wrote: > To my surprise, wikicode.get_parent() does not get you the section a node > is part of: > > > import mwparserfromhell as mwp > > > > text = """==foo== > > {{Template:Foo}} > > """ > > wikicode = mwp.parse(text) > > print(wikicode.get_tree()) > > > > print('++++++++++') > > > > node = wikicode.nodes[-2] > > print(f"{node=}") > > print(f"{wikicode.get_parent(node)=}") > > prints: > > > == > > foo > > == > > \n > > {{ > > Template:Foo > > }} > > \n > > ++++++++++ > > node='{{Template:Foo}}' > > wikicode.get_parent(node)=None > > Am I just doing this wrong? > _______________________________________________ > pywikibot mailing list -- pywikibot@lists.wikimedia.org > Public archives at > https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/message/ZFCIVR24X5L4SYPFA4KKGU3YQTODPS3E/ > To unsubscribe send an email to pywikibot-le...@lists.wikimedia.org >
_______________________________________________ pywikibot mailing list -- pywikibot@lists.wikimedia.org Public archives at https://lists.wikimedia.org/hyperkitty/list/pywikibot@lists.wikimedia.org/message/L7J2BBPWGWOLY5TOCCYPBUERCDJZDNX7/ To unsubscribe send an email to pywikibot-le...@lists.wikimedia.org