P
P
Peterson_s2020-06-18 11:43:10
Python
Peterson_s, 2020-06-18 11:43:10

How to pull data from xml response in Python?

import hashlib
import requests
def privat_bank():

    
    url = "https://api.privatbank.ua/p24api/rest_fiz"

    password = "апрваравпрвапрвапр"

    head = """<?xml version="1.0" encoding="UTF-8"?>
        <request version="1.0">
        <merchant>
        <id>00000</id>
        <signature>"""

    data = """<oper>cmt</oper>
                    <wait>0</wait>
                    <test>0</test>
                    <payment id="">
                        <prop name="sd" value="5.05.2020" />
                        <prop name="ed" value="17.06.2020" />
                        <prop name="card" value="012345678910" />
                    </payment>"""

    end_head = """</signature>
            </merchant>
            <data>
                """

    footer = """
    </data>
    </request>"""

    signature_md5 = hashlib.md5((data+password).encode('utf-8')).hexdigest()
    signature_done = hashlib.sha1(signature_md5.encode('utf-8')).hexdigest()

    data_done = head + signature_done + end_head + data + footer


    res = requests.post(url, data=data_done, headers={'Content-Type': 'application/xml; charset=UTF-8'})
    
    
    
    print(res.text)
  

  
def main():
    privat_bank()


main()


Here is the answer:
<?xml version="1.0" encoding="UTF-8"?><response version="1.0"><merchant><id>000000</id><signature>5d7fcffa2290b7ed4ad98ff259d6b79420f01ce5</signature></merchant><data><oper>cmt</oper><info><statements status="excellent" credit="0.0" debet="107.0">
<statement card="012345678910" appcode="00000" trandate="2020-05-15" trantime="13:14:00" amount="92.00 UAH" cardamount="-92.00 UAH" rest="4363.19 UAH" terminal="LIQPAY*Privat24 Androi, I0110UWI" description="Пополнение мобильного +380970000000"/>
<statement card="012345678910" appcode="0000" trandate="2020-05-10" trantime="00:02:00" amount="15.00 UAH" cardamount="-15.00 UAH" rest="4455.19 UAH" terminal="PrivatBank, C400" description="Перевод на карту ПриватБанка через приложение Приват24. Получатель: Іванов Іван Іванович"/>
</statements></info></data></response>

How to pull it out into a variable, rest or description?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-18
@Peterson_s

import xml.etree.ElementTree as ET

data = '''<?xml version="1.0" encoding="UTF-8"?><response version="1.0"><merchant><id>000000</id><signature>5d7fcffa2290b7ed4ad98ff259d6b79420f01ce5</signature></merchant><data><oper>cmt</oper><info><statements status="excellent" credit="0.0" debet="107.0">
<statement card="012345678910" appcode="00000" trandate="2020-05-15" trantime="13:14:00" amount="92.00 UAH" cardamount="-92.00 UAH" rest="4363.19 UAH" terminal="LIQPAY*Privat24 Androi, I0110UWI" description="Пополнение мобильного +380970000000"/>
<statement card="012345678910" appcode="0000" trandate="2020-05-10" trantime="00:02:00" amount="15.00 UAH" cardamount="-15.00 UAH" rest="4455.19 UAH" terminal="PrivatBank, C400" description="Перевод на карту ПриватБанка через приложение Приват24. Получатель: Іванов Іван Іванович"/>
</statements></info></data></response>'''

root = ET.fromstring(data)
statement = root.find('data/info/statements/statement')
rest = statement.get('rest')
description = statement.get('description')

If you need several, similarly
statements = root.find('data/info/statements')
for statement in statements:
    print(statement.get('rest'))
    print(statement.get('description'))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question