[python] HTTPie를 사용하여 서버 응답의 XML을 파싱하는 방법은 어떻게 되나요?

먼저, HTTPie를 설치해야 합니다. 다음 명령을 사용하여 설치할 수 있습니다:

$ pip install httpie

이제 HTTPie를 사용하여 서버에 요청하고 응답을 받아오겠습니다. 다음 명령을 사용합니다:

$ http GET http://example.com/api/data.xml

이제 XML 응답을 파싱하기 위해 파이썬 모듈인 xml.etree.ElementTree를 사용할 수 있습니다. 다음은 파싱하는 간단한 예제입니다:

import xml.etree.ElementTree as ET

response = """
<root>
    <item>
        <name>Apple</name>
        <price>1.99</price>
    </item>
    <item>
        <name>Orange</name>
        <price>2.49</price>
    </item>
</root>
"""

root = ET.fromstring(response)

for item in root.findall('item'):
    name = item.find('name').text
    price = item.find('price').text
    print(f"상품명: {name}, 가격: {price}")

실제 서버 응답을 받았다면 response 변수에 해당 값을 할당하면 됩니다. fromstring() 함수를 사용하여 XML 문자열을 Element 객체로 변환합니다. findall()find() 메소드를 사용하여 원하는 엘리먼트를 찾고, .text 속성을 통해 값을 가져올 수 있습니다. 예제에서는 각 item 엘리먼트에서 nameprice 값을 추출하고 출력합니다.

실제 서버와 통신하여 XML을 파싱하려면 HTTPie를 사용하여 서버 응답을 받고, response 변수에 해당 값을 할당하여 사용하면 됩니다.