簡體   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