python - XML error object has no attribute 'cssselect' -
the following not appear parse xml data correctly or doing wrong.
this code being run.
from lxml import etree lxml.etree import fromstring
if request.post: xml = request.post['xml'].encode('utf-8') parser = etree.xmlparser(ns_clean=true, recover=true, encoding='utf-8') h = fromstring(xml, parser=parser) status = h.cssselect('itagg_delivery_receipt status').text_content() return httpresponse(status)
the error:
attributeerror: 'lxml.etree._element' object has no attribute 'cssselect' status
this xml document being sent:
<?xml version="1.1" encoding="iso-8859-1"?> <itagg_delivery_receipt> <version>1.0</version> <msisdn>447889000000</msisdn> <submission_ref> 845tgrgsehg394g3hdfhhh56445y7ts6</ submission_ref> <status>delivered</status> <reason>4</reason> <timestamp>20050709120945</timestamp> <retry>0</retry> </itagg_delivery_receipt>
i have dumped out str(h) , looks <element 0x7fd341e93870="" at="" itagg_delivery_receipt=""></element>
here's how thing want xpath
:
>>> h.xpath('status/text()') ['delivered']
so little helper function in code:
def first(seq,default=none): item in seq: return item return default ... status = first(h.xpath('status/text()'))
Comments
Post a Comment