【问题标题】:html form to remote site, fetch the single result and display in my pagehtml 表单到远程站点,获取单个结果并显示在我的页面中
【发布时间】:2016-12-02 16:38:18
【问题描述】:

我有网站 mysite.com,需要使用 html 表单从另一个网站获取内容并将结果返回到我的网站。该表单不使用方法 GET 而是使用方法 POST ...

我创建了一个网络表单,用于在智利警方的网站从我的网站上查找被盗汽车。这是表格

<form action="http://consultawebvehiculos.carabineros.cl/index.php" 
name="form1" id="control" method="post">
<input type="hidden" name="accion" value="buscar">
<table id="SEARCH">
<tr><td class="REGISTRATION">
<input name="txtLetras" type="text" class="PLATE" maxlength="2">
</td><td class="SEP">°</td>
<td class="REGISTRATION">
<input name="txtNumeros1" type="text" class="PLATE" maxlength="2">
</td><td class="SEP">°</td>
<td class="REGISTRATION">
<input name="txtNumeros2" type="text" class="PLATE" maxlength="2">
</td></tr></table>
<div id="START" onClick="document.forms['control'].submit();">
CONSULTAR EN EL REGISTRO</div></form>

表单必须从

获取结果
http://consultawebvehiculos.carabineros.cl/index.php

结果显示在哪里(在警察局)

<p id="u973-2">
         La patente consultada <span class="Estilo1">NO</span> presenta Encargo..    </p>

我必须得到这个结果并显示在我的页面上,获取它。 我知道如何获取静态内容,但如何获取这种结果? 有什么想法吗?

我们至少有 div ID 名称,并且在此页面上始终是相同的名称...

尝试使用 AJAX

<script src="//code.jquery.com/jquery-latest.js"></script> <!-- load jquery via CDN -->
<script>
   $(document).ready(function() {//start document ready
      $('#start').click(function (e){
        e.preventDefault();

        $.ajax({
        type: 'POST',
        url: 'http://consultawebvehiculos.carabineros.cl/index.php',
        data: $("#cdr-form").serialize(),
success: function(d){
   $(".result").html(d);
}
    });
  });
 });//end document ready
</script>

【问题讨论】:

标签: php jquery html css ajax


【解决方案1】:

由于 CORS 问题,预计 Ajax 无法正常工作。

使用python

from mechanize import Browser

br = Browser()
br.open("http://consultawebvehiculos.carabineros.cl/index.php")
br.select_form(name="form1")

br["txtLetras"] = ''
br["txtNumeros1"] = ''
br["txtNumeros2"] = ''

response = br.submit()

# The following gives you full html response
print response.read()

# The following gives you short response info
print response.info()

当我使用response.info() 测试脚本时的示例响应信息:

Date: Fri, 02 Dec 2016 17:04:06 GMT
Server: Apache/2.2.0 (Unix) PHP/5.1.2
X-Powered-By: PHP/5.1.2
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html
Content-type: text/html;charset=UTF-8

好读:http://wwwsearch.sourceforge.net/mechanize/doc.html

我个人最喜欢的是Scrapy,它可以做很多很酷的事情!

使用 PHP

<?php

$url = 'http://consultawebvehiculos.carabineros.cl/index.php';

$fields = array(
  'txtLetras' => '',
  'txtNumeros1' => '',
  'txtNumeros2' => '',
);

$fields_string = '';

foreach($fields as $key=>$value) { 
  $fields_string .= $key.'='.$value.'&'; 
}

rtrim($fields_string, '&');
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);
print $result;

//close connection
curl_close($ch);

参考:https://davidwalsh.name/curl-post

【讨论】:

  • 用 PHP 有什么办法吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-24
  • 1970-01-01
  • 2014-06-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-06
相关资源
最近更新 更多