我创建了一个算法,它使用Python 3.3.3中的url lib.request
来使用本地银行的Web服务,但是当我运行它时会发出错误。
错误如下:
Traceback (most recent call last):
File "<pyshell#20>", line 1, in <module>
tipo_de_cambio()
File "/Users/admin/Documents/TEC/Taller Programación/tdc.py", line 13, in tipo_de_cambio
f = urllib.request.urlopen(request,data)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 375, in open
response = meth(req, response)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 487, in http_response
'http', request, response, code, msg, hdrs)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 413, in error
return self._call_chain(*args)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 347, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/urllib/request.py", line 495, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500: Internal Server Error
我编码的程序如下:
import datetime
import urllib.request
from urllib.error import HTTPError
from xml.dom.minidom import parse, parseString
def tipo_de_cambio():
a = 0
fecha = datetime.date.today()
data = urllib.parse.urlencode({'tcIndicador':'318', 'tcFechaInicio':fecha.strftime("%d/%m/%Y"), 'tcFechaFinal':fecha.strftime("%d/%m/%Y"), 'tcNombre':"Usuario", 'tnSubNiveles':'N'})
data = data.encode('utf-8')
request = urllib.request.Request('http://indicadoreseconomicos.bccr.fi.cr/indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmx?op=ObtenerIndicadoresEconomicosXML')
request.add_header("POST","application/x-www-form-urlencoded;charset=utf-8")
f = urllib.request.urlopen(request,data)
data = f.read().decode('utf-8')
dom = parseString(data)
xmlTag = dom.getElementsByTagName('NUM_VALOR')[0].toxml()
xmlData = xmlTag.replace('<NUM_VALOR>','').replace('</NUM_VALOR>','')
a = float(xmlData)
return(a)
有人可以告诉我为什么会收到此错误吗?
答案 0 :(得分:0)
import datetime
import urllib.request
from xml.dom.minidom import parse, parseString
fecha = datetime.date.today().strftime("%d/%m/%Y")
base_url = 'http://indicadoreseconomicos.bccr.fi.cr/indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmx/ObtenerIndicadoresEconomicosXML?tcIndicador={tcIndicador}&tcFechaInicio={tcFechaInicio}&tcFechaFinal={tcFechaFinal}&tcNombre={tcNombre}&tnSubNiveles={tnSubNiveles}'
data = {
'tcIndicador':'318',
'tcFechaInicio':fecha,
'tcFechaFinal':fecha,
'tcNombre':"Usuario",
'tnSubNiveles':'N'
}
result = urllib.request.urlopen(base_url.format(**data)).read()
dom = parseString(result)
xmlTag = dom.getElementsByTagName('NUM_VALOR')[0].toxml()
xmlData = xmlTag.replace('<NUM_VALOR>','').replace('</NUM_VALOR>','')
a = float(xmlData)
return(a)
从这里,您可以使用您的方法来解析xml。