繁体   English   中英

urllib.request错误500 python 3.3.3

[英]urllib.request error 500 python 3.3.3

我创建了一个使用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)

银行的Web服务网址为: http : //indicadoreseconomicos.bccr.fi.cr/indicadoreseconomicos/WebServices/wsIndicadoresEconomicos.asmx?op=ObtenerIndicadoresEconomicosXML

有人可以告诉我为什么会出现此错误吗?

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。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM