<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,我有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
发布于 2020-06-25 07:26:58
lower-case()
和matches()
都需要lxml 2.0,但XPath只实现了XPath 1.0。
XPath 1.0中用于不区分大小写的匹配的习惯用法使用translate()
,
translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')
在与需要进行不区分大小写比较的字符串的小写版本进行比较之前,将大写字符映射到小写。
所以,在你的情况下,
response.xpath(f"//img[translate(@alt, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='{company_name.lower()}']")
你的另一个XPath也是如此。
https://stackoverflow.com/questions/62565382
复制相似问题