Re: : Re: getparent() fails with "AttributeError: 'list' object has no attribute 'getparent' "
Through trial and error, it looks like xpath() returns an array, even if only one element is found in the tree.
Behold the power of the great docs: https://lxml.de/xpathxslt.html#xpath "[...] XPath return values The return value types of XPath evaluations vary, depending on the XPath expression used: - True or False, when the XPath expression has a boolean result - a float, when the XPath expression has a numeric result (integer or float) - a 'smart' string (as described below), when the XPath expression has a string result. - a list of items, when the XPath expression has a list as result. The items may include Elements (also comments and processing instructions), strings and tuples. Text nodes and attributes in the result are returned as 'smart' string values. Namespace declarations are returned as tuples of strings: (prefix, URI). [...]" ;-) Best regards, Holger Landesbank Baden-Wuerttemberg Anstalt des oeffentlichen Rechts Hauptsitze: Stuttgart, Karlsruhe, Mannheim, Mainz HRA 12704 Amtsgericht Stuttgart HRA 4356, HRA 104 440 Amtsgericht Mannheim HRA 40687 Amtsgericht Mainz Die LBBW verarbeitet gemaess Erfordernissen der DSGVO Ihre personenbezogenen Daten. Informationen finden Sie unter https://www.lbbw.de/datenschutz.
Thank you, and to Jens too. The newbie in me (wrongly) assumed that xpath() returned a pointer to the element if only one was found in the HTML file, like find(). On 30/05/2022 09:30, Holger.Joukl@LBBW.de wrote:
Through trial and error, it looks like xpath() returns an array, even if only one element is found in the tree. Behold the power of the great docs: https://lxml.de/xpathxslt.html#xpath
"[...] XPath return values
The return value types of XPath evaluations vary, depending on the XPath expression used:
- True or False, when the XPath expression has a boolean result - a float, when the XPath expression has a numeric result (integer or float) - a 'smart' string (as described below), when the XPath expression has a string result. - a list of items, when the XPath expression has a list as result. The items may include Elements (also comments and processing instructions), strings and tuples. Text nodes and attributes in the result are returned as 'smart' string values. Namespace declarations are returned as tuples of strings: (prefix, URI). [...]"
;-)
participants (2)
-
Gilles
-
Holger.Joukl@LBBW.de