简体   繁体   English

从Web应用程序检索数据-POST请求

[英]Retrieve data from web app - POST request

Suppose that you have a web app, let's say a Django application. 假设您有一个Web应用程序,例如Django应用程序。

You need to retrieve data from a javascript button which is located on another remote website. 您需要从另一个远程网站上的javascript按钮检索数据。

Like this one cne On Consulte sus Datos Registro Electoral section, you see there is a button to search for a given number. 像这样的CNEConsulte SUS DATOS Registro选举部分,你会看到有一个按钮,搜索给定数。

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? 但是,我需要从Django模板加载此文件,假设我的模板中还有另一个表单,当有人在我的字段上输入数字时,我需要从我的应用向远程表单执行相同的请求,看看数字是否有效,有没有办法做到这一点?

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 如果您转到该地址并输入例如V147452787,则该网址将为新网址

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. 您会看到那里显示的数据,我从django调用它并验证该字段没有问题,现在,我需要执行相同的操作,但要使用cne网站上的该按钮。

I've found this url: 我找到了这个网址:

Cne's empty url If you add data like this CNE的空网址如果添加数据,如

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. 在第一个示例中,为了验证是否有结果,您在响应的开头使用了字符串“ 450”的存在来表示错误匹配。 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. 字符串'no se encuentra inscrita'的存在指示查找失败。 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'))

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

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