我正在嘗試response使用以下方法獲取所有標簽:
.//{DAV:}response
tree = etree.fromstring(r.content)
for response in tree.xpath(".//{DAV:}response"):
...
問題是lxml回傳此錯誤:
lxml.etree.XPathEvalError: Invalid expression
我試圖避免定義命名空間,因為每個 CardDav 服務器都有不同的命名空間(誰知道為什么)。
這個表達有什么問題?
XML:
<?xml version='1.0' encoding='utf-8'?>
<multistatus xmlns="DAV:" xmlns:CR="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/">
<response>
<href>/admin/</href>
<propstat>
<prop>
<resourcetype>
<principal/>
<collection/>
</resourcetype>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
<propstat>
<prop>
<displayname/>
<CS:getctag/>
</prop>
<status>HTTP/1.1 404 Not Found</status>
</propstat>
</response>
<response>
<href>/admin/yyyyyy/</href>
<propstat>
<prop>
<resourcetype>
<CR:addressbook/>
<collection/>
</resourcetype>
<displayname>addressbook1</displayname>
<CS:getctag>"xxxxx"</CS:getctag>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
</response>
</multistatus>
uj5u.com熱心網友回復:
考慮為決議該默認命名空間下的節點分配一個臨時前綴,該前綴應該適用于xpathand findall:
tree = etree.fromstring(r.content)
nsmp = {'doc': 'DAV:'}
for response in tree.xpath(".//doc:response", namespaces=nsmp):
...
for response in tree.findall(".//doc:response", namespaces=nsmp):
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/484391.html
