Here's how I parse the xml response from this url
import requests
from xml.etree import ElementTree as ET
response = requests.get('https://www.lb.lt/webservices/FxRates/FxRates.asmx/getCurrencyList')
tree = ET.fromstring(response.content)
The response contains (truncated):
b'<?xml version="1.0" encoding="utf-8"?>\r\n<CcyTbl xmlns="http://www.lb.lt/WebServices/FxRates">\r\n <CcyNtry>\r\n <Ccy>ADP</Ccy>\r\n <CcyNm lang="LT">Andoros peseta</CcyNm>\r\n <CcyNm lang="EN">Andorran peseta</CcyNm>\r\n <CcyNbr>020</CcyNbr>\r\n <CcyMnrUnts>0</CcyMnrUnts>\r\n </CcyNtry>\r\n <CcyNtry>\r\n <Ccy>AED</Ccy>\r\n <CcyNm lang="LT">Jungtini\xc5\xb3 Arab\xc5\xb3 Emirat\xc5\xb3 dirhamas</CcyNm>\r\n <CcyNm lang="EN">UAE dirham</CcyNm>\r\n <CcyNbr>784</CcyNbr>\r\n <CcyMnrUnts>2</CcyMnrUnts>\r\n </CcyNtry>\r\n <CcyNtry>\r\n <Ccy>AFN</Ccy>\r\n <CcyNm lang="LT">Afganistano afganis</CcyNm>\r\n <CcyNm lang="EN">Afghani</CcyNm>\r\n <CcyNbr>971</CcyNbr>\r\n <CcyMnrUnts>2</CcyMnrUnts>\r\n </CcyNtry>\r\n <CcyNtry>\r\n <Ccy>ALL</Ccy>\r\n <CcyNm lang="LT">Albanijos lekas</CcyNm>\r\n <CcyNm lang="EN">Albanian lek</CcyNm>\r\n <CcyNbr>008</CcyNbr>\r\n <CcyMnrUnts>2</CcyMnrUnts>\r\n </CcyNtry>\r\n <CcyNtry>\r\n <Ccy>AMD</Ccy>\r\n ...'
From which I need to extract the available currencies in <Ccy>
tags. However it's not clear to me why the tags are not found:
>>> tree.findall('CcyNtry')
[]
>>> tree.findall('Ccy')
[]
What I'm trying to do is to access the results printed by the below
>>> for element in tree:
... print(element[0].text)
ADP
AED
AFN
ALL
AMD
ANG
AOA
ARS
ATS
AUD
AWG
AZN
BAM
BBD
BDT
BEF
BGN
BHD
BIF
BYN
BYR
BMD
BND
BOB
BOV
BRL
BSD
BTN
BWP
BZD
CAD
CDF
CHF
CYP
CLF
CLP
CNY
COP
CRC
CUP
CVE
CZK
DEM
DJF
DKK
DOP
DZD
ECS
ECV
EEK
EGP
ERN
ESP
ETB
EUR
FIM
FJD
FKP
FRF
GBP
GEL
GHS
GYD
GIP
GMD
GNF
GRD
GTQ
GWP
HKD
HNL
HRK
HTG
HUF
IDR
IEP
YER
ILS
INR
IQD
IRR
ISK
ITL
JMD
JOD
JPY
KES
KGS
KHR
KYD
KMF
KPW
KRW
KWD
KZT
LAK
LBP
LYD
LKR
LRD
LSL
LTL
LUF
LVL
MAD
MDL
MDR
MGA
MYR
MKD
MMK
MNT
MOP
MRO
MTL
MUR
MVR
MWK
MXN
MXV
MZN
NAD
NGN
NIO
NLG
NOK
NPR
NZD
OMR
PAB
PEN
PGK
PHP
PYG
PKR
PLN
PTE
QAR
RON
RSD
RUB
RWF
SAR
SBD
SCR
SDD
SEK
SGD
SHP
SYP
SIT
SKK
SLL
SOS
SRD
SSP
STD
SVC
SZL
THB
TJR
TJS
TMT
TND
TOP
TPE
TRY
TTD
TWD
TZS
UAH
UGX
UYU
USD
USN
UZS
VEB
VES
VND
VUV
WST
XAF
XAG
XAU
XBA
XBB
XBC
XBD
XCD
XDR
XFO
XFU
XOF
XPD
XPF
XPT
XXX
ZAR
ZMW
ZRN
ZWL
CodePudding user response:
Unfortunately, you have to deal with the namespace in the file. So try it this way:
namespaces = {'xx': 'http://www.lb.lt/WebServices/FxRates'}
for element in tree.findall('.//xx:CcyNtry/xx:Ccy', namespaces):
print(element.text)
The output should be what you're looking for.