只有两个问题:
答案 0 :(得分:5)
例如:
from lxml import etree
from StringIO import StringIO
tree = etree.parse(StringIO('<foo><bar></bar></foo>'))
try:
tree.xpath('\BAD XPATH')
print '1. Valid XPath'
except etree.XPathEvalError, e:
print '1. Invalid XPath: ', e
if not tree.xpath('/foo/xxx'):
print '2. Requested node does not exist.'
运行如下:
1. Invalid XPath: Invalid expression
2. Requested node does not exist.