LeandroDiaz96 LeandroDiaz96 - 2 months ago 38
Python Question

How to extract one value from an xml url using xml.etree

Im trying to print the value of just one field of a XML tree, here is the XML tree (e.g), the one that i get when i request it

<coberturaSocial>O.S.P. TIERRA DEL FUEGO(IPAUSS)</coberturaSocial>
<denominacion>DAMIAN GUTIERREZ DEL RIO</denominacion>

Now, i just want to print "coberturaSocial" value, here the request that i have in my views.py:

def get(request):
r = requests.get('https://sisa.msal.gov.ar/sisa/services/rest/puco/38785898')
dom = r.content
asd = etree.fromstring(dom)

If i print "asd" i get this error: The view didn't return an HttpResponse object. It returned None instead.
and also in the console i get this
I just want to print coberturaSocial, please help, new in xml parsing!


You need to extract the contents of the tag and then return it wrapped in a response, like so:

return HttpResponse(asd.find('coberturaSocial').text)