Python 值错误 : XPath error: Unregistered function

标签 python html xml xpath

<img alt="MediaMarkt" border="0" e-editable="img" src="http://news-de.mediamarkt.de/custloads/298149669/vce/mediamarkt.png" style="display:block;" width="169"/>

我试图从 HTML 获取 src,我有 alt 值,然后使用它我尝试获取图像

company_name = "mediamarkt"
response.xpath(f'//img[lower-case(@alt)="{company_name.lower()}"]') #Error
response.xpath(f"//img[matches(@alt,'{company_name}','i')]") # Error

我得到的错误:

Traceback (most recent call last):
  File "/home/timmy/.local/lib/python3.8/site-packages/parsel/selector.py", line 254, in xpath
    result = xpathev(query, namespaces=nsp,
  File "src/lxml/etree.pyx", line 1582, in lxml.etree._Element.xpath
  File "src/lxml/xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
  File "src/lxml/xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
lxml.etree.XPathEvalError: Unregistered function

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/home/timmy/.local/lib/python3.8/site-packages/scrapy/http/response/text.py", line 117, in xpath
    return self.selector.xpath(query, **kwargs)
  File "/home/timmy/.local/lib/python3.8/site-packages/parsel/selector.py", line 260, in xpath
    six.reraise(ValueError, ValueError(msg), sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/six.py", line 702, in reraise
    raise value.with_traceback(tb)
  File "/home/timmy/.local/lib/python3.8/site-packages/parsel/selector.py", line 254, in xpath
    result = xpathev(query, namespaces=nsp,
  File "src/lxml/etree.pyx", line 1582, in lxml.etree._Element.xpath
  File "src/lxml/xpath.pxi", line 305, in lxml.etree.XPathElementEvaluator.__call__
  File "src/lxml/xpath.pxi", line 225, in lxml.etree._XPathEvaluatorBase._handle_result
ValueError: XPath error: Unregistered function in //img[matches(@alt,'mediamarkt','i')]

我从 case-insensitive matching in xpath? 得到了那些 XPath

最佳答案

lower-case()matches() 都需要 XPath 2.0,但 lxml 仅实现 XPath 1.0。

XPath 1.0 中用于不区分大小写匹配的习语使用 translate(),

translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')

在与需要不区分大小写的比较的字符串的小写版本进行比较之前,将大写字符映射为小写字符。

所以,在你的情况下,

response.xpath(f"//img[translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='{company_name.lower()}']")

对于您的其他 XPath 也是如此。

另见 case insensitive xpath contains() possible?

关于Python 值错误 : XPath error: Unregistered function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62565382/

相关文章:

python - 使用 xml.dom.minidom 计算 python 中 xml 标签的数量

python - 创建子图而不是单独的图

python - 无法在 anaconda python 2.7 中导入 tensorflow

javascript - 变量没有符号的语言如何处理动态调度/调用?

javascript - 如何比较文本框与日期选择器

javascript - 控制台错误 : 'Uncaught SyntaxError: Unexpected token : Learning sprites'

jquery - 如何拖动一个元素,但它仍应保留在原来的位置?

xml - Odoo 路由给出 "Internal Server Error"错误页面

java - 如何使用相同的 JAXB 类将其编码和解码到不同的命名空间中?

python - 为什么在Pillow-python中调整图像大小会删除Image.format?