Python - How to parse xml response and store a elements value in a variable?

ぐ巨炮叔叔 提交于 2020-08-26 09:28:08

问题


enter code hereI am getting the XML response from the API call.

I need the "testId" attribute value from this response, Please help me on this.

   r=requests.get( myconfig.URL_webpagetest + "?url=" + testurl + "&f=xml&k=" + myconfig.apikey_webpagetest )

    xmltxt=r.content

    print (xmltxt)

    testId = XML (xmltxt).find("testId").text

    r=requests.get("http://www.webpagetest.org/testStatus.php?f=xml&test=" + testId )

xml response -

<?xml version="1.0" encoding="UTF-8"?>
<response>
<statusCode>200</statusCode>
<statusText>Ok</statusText>
<data>
<testId>180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</testId>
<ownerKey>dfc65d98de13c4770e528ef5b65e9629a52595e9</ownerKey>
<jsonUrl>http://www.webpagetest.org/jsonResult.php?test=180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</jsonUrl>
</data>
</response>

The following error is responding -

Traceback (most recent call last): File "/pagePerformance.py", line 52, in testId = XML (xmltxt).find("testId").text AttributeError: 'NoneType' object has no attribute 'text'


回答1:


Use the following to collect testId from response:-

import xml.etree.ElementTree as ET

response_xml_as_string = "xml response string from API"
responseXml = ET.fromstring(response_xml_as_string)
testId = responseXml.find('data').find('testId')
print testId.text



回答2:


from lxml.etree import fromstring

string = '<?xml version="1.0" encoding="UTF-8"?> <response> <statusCode>200</statusCode> <statusText>Ok</statusText> <data><testId>180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</testId> <ownerKey>dfc65d98de13c4770e528ef5b65e9629a52595e9</ownerKey> <jsonUrl>http://www.webpagetest.org/jsonResult.php?test=180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</jsonUrl> </data> </response>'

response = fromstring(string.encode('utf-8'))
elm = response.xpath('/response/data/testId').pop()
testId = elm.text

This way you can search for any element within the xml from the root/parent element via the XPATH.

Side Note: I don't particular like using the pop method to remove the item from a single item list. So if anyone else has a better way to do it please let me know. So far I've consider:

1) elm = next(iter(response.xpath('/response/data/testId')))

2) simply leaving it in a list so it can use as a stararg




回答3:


I found this article the other day when it appeared on my feed, and it may suit your needs. I skimmed it, but in general the package parses xml data and converts the tags/attributes/values into a dictionary. Additionally, the author points out that it maintains the nesting structure of the xml as well.

https://www.oreilly.com/learning/jxmlease-python-xml-conversion-data-structures

for your use case.

>>> xml = '<?xml version="1.0" encoding="UTF-8"?> <response> <statusCode>200</statusCode> <statusText>Ok</statusText> <data> <testId>180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</testId> <ownerKey>dfc65d98de13c4770e528ef5b65e9629a52595e9</ownerKey> <jsonUrl>http://www.webpagetest.org/jsonResult.php?test=180523_YM_054fd7d84fd4ea7aed237f87289e0c7c</jsonUrl> </data> </response>'

>>> root = jxmlease.parse(xml)
>>> testid = root['response']['data']['testId'].get_cdata()
>>> print(testid)
>>> '180523_YM_054fd7d84fd4ea7aed237f87289e0c7c'


来源:https://stackoverflow.com/questions/50482088/python-how-to-parse-xml-response-and-store-a-elements-value-in-a-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!