如何使用xml从xml url中提取一个值。

2024-09-29 00:19:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图只打印XML树中一个字段的值,这是XML树(例如,当我请求它时得到的)

<puco>
    <resultado>OK</resultado>
    <coberturaSocial>O.S.P. TIERRA DEL FUEGO(IPAUSS)</coberturaSocial>
    <denominacion>DAMIAN GUTIERREZ DEL RIO</denominacion>
    <nrodoc>32443324</nrodoc>
    <rnos>924001</rnos>
    <tipodoc>DNI</tipodoc>
</puco>

现在,我只想打印“coberturaSocial”值,这里是我在视图.py公司名称:

^{pr2}$

如果我打印“asd”,就会得到一个错误:视图没有返回HttpResponse对象。它没有返回任何结果。 在控制台里我也看到了这个 我只想打印coberturaSocial,请帮忙,新的xml解析!在


Tags: 视图okxmldeldamianresultadopucocoberturasocial
2条回答

您需要提取标记的内容,然后将其包装在响应中返回,如下所示:

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

我猜etreeimport xml.etree.ElementTree as etree

您可以使用:

text = r.content
dom = etree.fromstring(text)
el = dom.find('coberturaSocial')
el.text # this is where the string is

相关问题 更多 >