Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python ValueError:XPath错误:未注册函数_Python_Html_Xml_Xpath - Fatal编程技术网

Python ValueError:XPath错误:未注册函数

Python ValueError:XPath错误:未注册函数,python,html,xml,xpath,Python,Html,Xml,Xpath,我得到的错误是: company_name = "mediamarkt" response.xpath(f'//img[lower-case(@alt)="{company_name.lower()}"]') #Error response.xpath(f"//img[matches(@alt,'{company_name}','i')]") # Error 回溯(最近一次呼叫最后一次): xpath中的文件“/home/timmy/

我得到的错误是:

company_name = "mediamarkt"
response.xpath(f'//img[lower-case(@alt)="{company_name.lower()}"]') #Error
response.xpath(f"//img[matches(@alt,'{company_name}','i')]") # Error
回溯(最近一次呼叫最后一次):
xpath中的文件“/home/timmy/.local/lib/python3.8/site packages/parsel/selector.py”,第254行
结果=xpathev(查询,名称空间=nsp,
lxml.etree.\u Element.xpath中的文件“src/lxml/etree.pyx”,第1582行
文件“src/lxml/xpath.pxi”,第305行,位于lxml.etree.XPathElementEvaluator中__
文件“src/lxml/xpath.pxi”,第225行,在lxml.etree.\u xpatheevaluorbase.\u handle\u result中
lxml.etree.xpathevaleror:未注册的函数
在处理上述异常期间,发生了另一个异常:
回溯(最近一次呼叫最后一次):
文件“/usr/lib/python3.8/code.py”,第90行,运行代码
exec(代码,self.locals)
文件“”,第1行,在
xpath中的文件“/home/timmy/.local/lib/python3.8/site packages/scrapy/http/response/text.py”,第117行
返回self.selector.xpath(查询,**kwargs)
xpath中的文件“/home/timmy/.local/lib/python3.8/site packages/parsel/selector.py”,第260行
重新发送(ValueError,ValueError(msg),sys.exc_info()[2])
文件“/usr/lib/python3/dist-packages/six.py”,第702行,在reraise中
通过_回溯(tb)提升值
xpath中的文件“/home/timmy/.local/lib/python3.8/site packages/parsel/selector.py”,第254行
结果=xpathev(查询,名称空间=nsp,
lxml.etree.\u Element.xpath中的文件“src/lxml/etree.pyx”,第1582行
文件“src/lxml/xpath.pxi”,第305行,位于lxml.etree.XPathElementEvaluator中__
文件“src/lxml/xpath.pxi”,第225行,在lxml.etree.\u xpatheevaluorbase.\u handle\u result中
ValueError:XPath错误://img[matches(@alt,'mediamarkt','i')中未注册的函数
我从

获得了这些XPath,
lower-case()
matches()
都需要XPath2.0,但lxml只实现了XPath1.0

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

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

所以,在你的情况下

translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
对于另一个XPath也是如此


另请参见

我更新了答案,向您展示了如何具体应用于您的案例。
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')]
translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
response.xpath(f"//img[translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='{company_name.lower()}']")