我正在尝试使用Mechanize
模仿填写表单。但是,它是一个ASP网页,即使我认为我正在正确处理doPostbacks我仍然使用手动填写页面时工作正常的信息得到错误(错误是页面说它'正在进行日常维护',这必须发生,因为我错过了必需的参数。)
当我手动提交表单时,这是向服务器发出的请求(可在Chrome Inspector工具的“网络”标签中找到):
Remote Address:209.67.234.13:80
Request URL:http://www.luckystarbus.com/Purchase.aspx
Request Method:POST
Status Code:302 Found
Request Headersview parsed
POST /Purchase.aspx HTTP/1.1
Host: www.luckystarbus.com
Connection: keep-alive
Content-Length: 3450
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://www.luckystarbus.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.101 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://www.luckystarbus.com/Purchase.aspx
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8,es;q=0.6
Cookie: ASP.NET_SessionId=2mw5a43zz43nemyj5e4glq45; _gat=1; _ga=GA1.2.181151250.1412747640
Form Dataview sourceview URL encoded
__EVENTTARGET:
__EVENTARGUMENT:
__LASTFOCUS:
__VIEWSTATE:/wEPDwUJMzcyMTg0Mjk0D2QWAmYPZBYCAgMPZBYGAgMPDxYCHgtOYXZpZ2F0ZVVybAUdL1B1cmNoYXNlLmFzcHg/TGFuZ3VhZ2U9ZXMtTVhkZAIGDw8WAh4EVGV4dAXmCDxhIGhyZWY9Ii9wcm9tby9OWW1hcC5wZGYiIHRhcmdldD1fYmxhbms+PHU+PEZPTlQgQ09MT1I9I0ZGMDAwMCBTSVpFPTU+QVRURU5USU9OISEgTkVXIFlPUksgVElDS0VUIENPVU5URVIgTE9DQVRJT04gSEFTIE1PVkVEIFRPIDE0NSBDQU5BTCBTVFJFRVQ8QlI+IA0KPEJSPllvdSBNVVNUIGNoZWNrIGluIGF0IDE0NSB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EVENTVALIDATION:/wEWNwKs4av/BwLuseWwCgLV3sKgCwKn3JGKDQLJj5y8CgLIj5y8CgLLj5y8CgLKj5y8CgLNj5y8CgLMj5y8CgLPj5y8CgLej5y8CgLRj5y8CgLJj9y/CgLJj9C/CgLJj9S/CgLJj+i/CgLJj+y/CgLJj+C/CgLJj+S/CgLJj/i/CgLJj7y8CgLJj7C8CgLIj9y/CgLIj9C/CgLIj9S/CgLIj+i/CgLIj+y/CgLIj+C/CgLIj+S/CgLIj/i/CgLIj7y8CgLIj7C8CgLLj9y/CgLLj9C/CgLLj9S/CgLLj+i/CgLLj+y/CgLLj+C/CgLLj+S/CgLLj/i/CgLLj7y8CgLLj7C8CgLKj9y/CgKEqZSECQKEqZSECQKu98XRCAKppL+gBwKztP72DgKw7NHHAgLe4cH0AgLmvYqoAgLczuTwCAKkgd7NDALA6+GiC77wn7OOuuAuAIz5dzZgP6GxLs9S
ctl00$MainContent$rbTripType:One
ctl00$MainContent$numPassengers:1
ctl00$MainContent$ddDepartureCity:Boston, MA
ctl00$MainContent$ddArrivalCity:1
ctl00$MainContent$sd:10/17/2014
ctl00$MainContent$rbFareType:Lowest
ctl00$MainContent$btGo.x:64
ctl00$MainContent$btGo.y:13
所以,我试图使用Mechanize来模仿这些输入中的每一个:
def search
agent = Mechanize.new
url = "http://www.luckystarbus.com"
page = agent.get(url)
form = page.form("aspnetForm")
form.radiobutton_with(value: 'One').check
form.field_with(name: 'ctl00$MainContent$ddDepartureCity').options[1].select
form.add_field!('__EVENTARGUMENT', '')
form.add_field!('__EVENTTARGET', '')
form.add_field!('__LASTFOCUS', '')
form.add_field!('ctl00$MainContent$btGo.x',64)
form.add_field!('ctl00$MainContent$btGo.y',13)
form.field_with(name: 'ctl00$MainContent$sd').value = "10/24/2014"
form.radiobutton_with(value: 'Lowest').check
page = form.click_button
end
我还尝试按照另一个SO答案的建议发布参数,但得到了同样的错误:
def search
agent = Mechanize.new
agent.post("http://www.luckystarbus.com/Purchase.aspx", {
'ctl00$MainContent$rbTripType'=>'One',
'ctl00$MainContent$numPassengers'=>'1',
'ctl00$MainContent$ddArrivalCity'=>'New%20York%2C+NY',
'ctl00$MainContent$ddDepartureCity'=>'Boston%2C+MA',
'ctl00$MainContent$sd'=>'10%2F24%2F2014',
'ctl00$MainContent$rbFareType'=>'Lowest',
'ctl00$MainContent$btGo.x'=>64,
'ctl00$MainContent$btGo.y'=>13,
'__EVENTARGUMENT'=>'',
'__EVENTTARGET'=>'',
'__LASTFOCUS'=>''
})
end
我想要回来的页面应该有“选择时间表”的标题。当以这种方式直接提交参数时,是否有一种方法我不知道?每次填写带有doPostback的输入时,我应该执行form.click_button
吗?
答案 0 :(得分:2)
是的,如果需要,您需要设置doPostBack值。 假设表格有:
<form onsubmit="doPostback('foo', 'bar')">
然后你可以做机械化:
form['__EVENTTARGET'] = 'foo'
form['__EVENTARGUMENT'] = 'bar'
如果仍然无法获取,请在浏览器中提出请求并从中提取值:
page = @agent.get 'http://www.luckystarbus.com/'
form = page.form
form['ctl00$MainContent$rbTripType'] = 'One'
form['ctl00$MainContent$numPassengers'] = '1'
form['ctl00$MainContent$ddDepartureCity'] = 'Boston, MA'
form['ctl00$MainContent$ddArrivalCity'] = '1'
form['ctl00$MainContent$sd'] = '10/17/2014'
form['ctl00$MainContent$rbFareType'] = 'Lowest'
form['__EVENTTARGET'] = 'ctl00$MainContent$rbTripType$1'
form['__EVENTARGUMENT'] = ''
form['__VIEWSTATE'] = '/wEPDwUJMzcyMTg0Mjk0D2QWAmYPZBYCAgMPZBYGAgMPDxYCHgtOYXZpZ2F0ZVVybAUdL1B1cmNoYXNlLmFzcHg/TGFuZ3VhZ2U9ZXMtTVhkZAIGDw8WAh4EVGV4dAXmCDxhIGhyZWY9Ii9wcm9tby9OWW1hcC5wZGYiIHRhcmdldD1fYmxhbms+PHU+PEZPTlQgQ09MT1I9I0ZGMDAwMCBTSVpFPTU+QVRURU5USU9OISEgTkVXIFlPUksgVElDS0VUIENPVU5URVIgTE9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