我试图使用Mechanize捕获POST请求,这是不可能通过窗体,因为窗体是在防止通过JavaScript直接加载的iframe中。使用机械化POST请求捕获
HTTP头中,谷歌Chrome浏览器为例请求如下(注意parada
和linea
参数)
Request URL:http://www.etr.gov.ar/getSmsResponse.php
Request Method:POST
Status Code:200 OK
Request Headers
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:es-419,es;q=0.8
Connection:keep-alive
Content-Length:21
Content-Type:application/x-www-form-urlencoded
Host:www.etr.gov.ar
Origin:http://www.etr.gov.ar
Referer:http://www.etr.gov.ar/cont-cuandollega.php
User-Agent:Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.30 Safari/534.30
X-Requested-With:XMLHttpRequest
Form Dataview URL
parada:4152
linea:112
Response Headers
Connection:close
Content-Length:111
Content-Type:text/html
Date:Fri, 03 Jun 2011 02:35:45 GMT
Server:Microsoft-IIS/7.5
X-Powered-By:PHP/5.1.2
ASP.NETl
而对于这个例子中,内容是:
Linea 112N: 0min. 379mts., siguiente 25min. 9937mts. - Linea 112R: 81min. 24349mts., siguiente 101min. 30548mts
我”什么已尝试机械化是以下ruby脚本,但我得到一个空白页作为回应:
require 'mechanize'
agent = WWW::Mechanize.new
agent.post("http://www.etr.gov.ar/getSmsResponse.php", "parada" => "4152", "linea"=>"112")
我会做错什么?非常感谢你。
更新:将POST作为散列传递完美。为了呈现内容,我只需要做agent.post.content
我投下来,因为有一个在路上没有区别的Ruby这个代码之间的解释参数和原始代码。这不是解决问题。 – Kai 2011-06-10 02:03:39
够公平的。感谢您指出这一点。我测试了它,你是对的。 – mightilybix 2011-06-27 13:33:55