Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
lxml 中的 xpath() 函数通常返回元素列表。
如果我有一个 XPath,我希望它只返回一个元素,那么最好的方法是:
我真的在寻找 SQLAlchemy 的 one() 函数的类似物。
try: (element,) = tree.xpath('//xpath/selector') except ValueError: raise InvalidSelector() # happened because the list was either empty or contained multiple elements