简体   繁体   中英

Retrieve data from web app - POST request

Suppose that you have a web app, let's say a Django application.

You need to retrieve data from a javascript button which is located on another remote website.

Like this one cne On Consulte sus Datos Registro Electoral section, you see there is a button to search for a given number.

It has this function attached:

<form name="formulario_rep" method="post" action="javascript:BuscaRE_CE(document.formulario_rep.nacionalidad.value, document.formulario_rep.cedula.value)">
    <table class="consulta_re" cellpadding="0" cellspacing="0">
    <tbody><tr>
        <td colspan="2" align="center" class="titulo_consulta_re">Consulte sus Datos</td>
    </tr>
    <tr>
        <td colspan="2" align="center" class="subtitulo_consulta_re">Registro Electoral</td>
    </tr>

            <tr>
        <td width="20" align="right">
            <select name="nacionalidad" class="formulario">
            <option value="V">V</option>
            <option value="E">E</option>
            </select>
        </td>
        <td width="32" align="left">
            <input name="cedula" type="text" class="formulario" maxlength="8" size="8" value="Cédula" onfocus="javascript:clear_textbox2();" onblur="javascript:Validar_Numero(document.formulario_rep.cedula);">
        </td>
    </tr>
    <tr>
        <td colspan="2" align="center">
            <input type="submit" value="Buscar" class="boton">
        </td>
    </tr>
                    <tr><td height="8"></td></tr>
    </tbody></table>
    </form>

However, I'll need to load this from a Django template, suppose I have another form into my template, when a person puts a number on my field, I need to do this very same request from my app, to the remote form, to see if numbers are valid or not, is there a way to do this?

EDIT

I have this code which validates data from another website:

def validate_vat(government_id, is_company):
    if government_id[0] == 'J' or government_id[0] == 'V':
        # Check on SENIATs page
        # TODO: What happens if this site is offline??
        url = 'http://contribuyente.seniat.gob.ve/getContribuyente/getrif?rif='
        s = requests.get(url + government_id).text
        if s.startswith('450'):
            raise ValidationError(_('ERROR: ' + s))

    elif is_company:
        raise ValidationError(_('Companies must provide VAT Numbers'))

    else:
        raise ValidationError(_("RIFs should start with either a "
                            "'J' or a 'V'"))

    return True

If you go to this address and put for example V147452787, so the url will be new url

You see data shown there, I call this from django and validates the field with no problems, now, I need to do the very same thing but with that button from cne website.

I've found this url:

Cne's empty url If you add data like this

Then you see some info, that is what I'm looking for...

You will have to change your validation function. In your first example, to validate whether there are results or not, you used the presence of the string '450' at the beginning of the response to denote a false match. For the new site, there are more stuff being sent back to you, so it is just a matter of testing the differences.

The presence of the string 'no se encuentra inscrita' indicates a failed lookup. Sample code below. You can save this excerpt as a file and run from console before you make changes and integrate to your code.

from __future__ import print_function
import requests

def validate(nationality, id):
    baseurl = 'http://www.cne.gob.ve/web/registro_electoral/ce.php'
    query_params = '?nacionalidad=%s&cedula=%s' % (nationality, id)
    url = baseurl + query_params
    print(url + ': ', end='')
    if nationality == 'V' or nationality == 'E':
        s = requests.get(url).text
        if 'no se encuentra inscrita' not in s:
            return True
    return False

print(validate('V', '13253891'))
print(validate('V', '132538911'))
print(validate('V', 'jhiw300'))
print(validate('E', '13253891'))
print(validate('E', '132538911'))
print(validate('E', 'jhiw300'))
print(validate('X', '13253891'))
print(validate('X', '132538911'))
print(validate('X', 'jhiw300'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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