help with web service in python

0

I have this web service, which should then give me a graph the code is all right and it gives me a graph without data, how do I enter the data and that I graph that data? I'm working on this code in colaboratory.

#importar graficador
import matplotlib.pyplot as plt

#importar datos de BCCR
import requests

#importar modulo procesar XML
import xml.etree.ElementTree as ET

tcIndicador = 317 #indicador del banco
tcFechaInicio = '1/1/2018'
tcFechaFinal = '1/2/2018'
tcNombre = 'Diego'
tnSubNiveles = 'N'   # S o N

#Consumir el web service del BCCR en un objeto de tipo requests.model.Response
#Más informacion en: http://docs.python-requests.org/en/master/user/quickstart/

r = requests.post("http://indicadoreseconomicos.bccr.fi.cr/indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmz/ObtenerIndicadoresEconomicos",
                 data={'tcIndicador': tcIndicador, 'tcFechaInicio': tcFechaInicio,
                      'tcFechaFinal': tcFechaFinal, 'tcNombre': tcNombre, 'tnSubNiveles':tnSubNiveles })

#Transforma XML es un :xml.etree.ElementTree
#Más información: https://docs.python.org/2/Library/xml.etree.elementtree.html

root = ET.fromstring ( r.text )
print( type( root ) )



matrix = []
print("---------------PASAR TODOS DATOS A MATRIX------------" )
for i in range( len( root[1][0] )):
  fila = []
  try:
       fila.append( root[1][0][i][0].text )
  except: fila.append( "NA" )
  try:
       fila.append( root[1][0][i][1].text )
  except: fila.append( "NA" )
  try:

       fila.append( root[1][0][i][2].text )
  except: fila.append( "NA" )
  matrix.append( fila )

lista_graficar= []
for i in range( len ( matrix ) ):
    #print(matrix[i][2] )
    if matrix[i][2] != "NA" and matrix[i][2]  !="0.00000000" :   
         lista_graficar.append( matrix[i][2] )

##Graficar la lista
# Más informacion: http://matplotlib.org/users/pyplot_tutorial.html
plt.plot( lista_graficar )
plt.ylabel( 'Tipo de cambio')
plt.show()
    
asked by libra 21.10.2018 в 23:38
source

1 answer

0

If you print the value of r.text you will discover the following:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>404 - File or directory not found.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;} 
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;} 
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;} 
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>404 - File or directory not found.</h2>
  <h3>The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.</h3>
 </fieldset></div>
</div>
</body>
</html>

That is, it is not an XML as you expected, but an HTML showing a 404 error. Make sure you are using the correct URL.

    
answered by 21.10.2018 в 23:51