lxml中的xpath()函数通常返回元素列表.
如果我有一个XPath,我希望它只返回一个元素,那么最好的方法是:
我真的在寻找SQLAlchemy的one()函数的模拟.
and*_*ean 18
try:
(element,) = tree.xpath('//xpath/selector')
except ValueError:
raise InvalidSelector()
# happened because the list was either empty or contained multiple elements
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1905 次 |
| 最近记录: |