Answer the question
In order to leave comments, you need to log in
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()
<?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>
Answer the question
In order to leave comments, you need to log in
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')
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 questionAsk a Question
731 491 924 answers to any question