lxml中的xpath()函数通常返回元素列表。
如果我有一个XPath,我希望它只返回一个元素,那么最好的方法是什么:
我真的在寻找SQLAlchemy的one()函数的模拟。
答案 0 :(得分:18)
try:
(element,) = tree.xpath('//xpath/selector')
except ValueError:
raise InvalidSelector()
# happened because the list was either empty or contained multiple elements