Web service in python

0

Hello, I am really new to this and I want to connect to the web service of the bank of Mexico to get the exchange rate, which I am doing in Python.

Until now I have managed to connect to the banxico web service with the following code:

from suds.client import Client

client = Client('http://www.banxico.org.mx:80/DgieWSWeb/DgieWS?WSDL')
#print client

response = client.service.tiposDeCambioBanxico()
print response

but he throws me all the exchange rates and I just need the dollar.

I hope someone can guide me to just take that value.

Thank you.

    
asked by Lizie R 23.11.2017 в 21:49
source

1 answer

0

You can process the xml obtained through xml.etree.ElementTree is the standard library and find the information you want using xpath:

import xml.etree.ElementTree as ET
from suds.client import Client

client = Client('http://www.banxico.org.mx:80/DgieWSWeb/DgieWS?WSDL')
xml_tree = ET.fromstring(client.service.tiposDeCambioBanxico().encode('Latin-1'))
res = xml_tree.find('.//*[@IDSERIE="SF60653"]/*')
data = res.attrib

print "TIME_PERIOD: {}\nOBS_VALUE: {}".format(data["TIME_PERIOD"], data["OBS_VALUE"])

Exit:

  

TIME_PERIOD: 2017-11-23
  OBS_VALUE: 18.8242

With the above we obtain the data corresponding to:

  

"Exchange rate pesos per US dollar Exchange rate to settle obligations denominated in foreign currency Settlement date"

If what you want is:

  

"Exchange rate pesos per US dollar Exchange rate to settle obligations denominated in foreign currency Date of determination (FIX)"

you should use:

res = xml_tree.find('.//*[@IDSERIE="SF43718"]/*')

The value of the attribute IDSERIE is used to filter the desired change.

We could also use regular expressions directly instead of resorting to parse the xml.

    
answered by 24.11.2017 в 02:46