Equivalent of element.xpath(xpath_str) but compile xpath_str once for all. See https://lxml.de/xpathxslt.html#xpath-return-values Args: * element (ElementBase): [description] * xpath_spec (str|lxml.etree.XPath): XPath as a str or lxml.etree.XPath Returns: * resu
(element, xpath_spec)
| 539 | |
| 540 | |
| 541 | def eval_xpath(element, xpath_spec): |
| 542 | """Equivalent of element.xpath(xpath_str) but compile xpath_str once for all. |
| 543 | See https://lxml.de/xpathxslt.html#xpath-return-values |
| 544 | |
| 545 | Args: |
| 546 | * element (ElementBase): [description] |
| 547 | * xpath_spec (str|lxml.etree.XPath): XPath as a str or lxml.etree.XPath |
| 548 | |
| 549 | Returns: |
| 550 | * result (bool, float, list, str): Results. |
| 551 | |
| 552 | Raises: |
| 553 | * TypeError: Raise when xpath_spec is neither a str nor a lxml.etree.XPath |
| 554 | * SearxXPathSyntaxException: Raise when there is a syntax error in the XPath |
| 555 | * SearxEngineXPathException: Raise when the XPath can't be evaluated. |
| 556 | """ |
| 557 | xpath = get_xpath(xpath_spec) |
| 558 | try: |
| 559 | return xpath(element) |
| 560 | except XPathError as e: |
| 561 | arg = ' '.join([str(i) for i in e.args]) |
| 562 | raise SearxEngineXPathException(xpath_spec, arg) from e |
| 563 | |
| 564 | |
| 565 | def eval_xpath_list(element, xpath_spec, min_len=None): |
no test coverage detected