从form.submit.search
即删除搜索form.submit
我猜你正在附加搜索以提交认为它与提交按钮的值有关,即搜索。
你正在做的代码是成功提交表单。但是,您正在使用 nil 参数调用结果页面对象的搜索方法。search 方法需要一个选择器,例如'body div#nav_bar ul.links li'
作为它的参数,以返回与该选择器匹配的元素数组。当然,没有元素会匹配 nil 选择器,因此是空数组。
根据您的回复进行编辑:
你的代码:
ruby script/console
require 'mechanize'
agent = WWW::Mechanize.new
agent.get("https://www.owens.edu/cgi-bin/class.pl/")
agent.page.forms
form = agent.page.forms.last
form.occ_subject = "chm"
form.submit.search
=> []
我尝试并开始工作:
红宝石脚本/控制台
require 'mechanize'
agent = WWW::Mechanize.new
agent.get("https://www.owens.edu/cgi-bin/class.pl")
agent.page.forms
form = agent.page.forms.last
form.occ_subject = "chm"
form.submit # <- No search method.
=> Insanely long array of HTML elements
相同的代码也不适用于 Google:
require 'mechanize'
require 'nokogiri'
agent = WWW::Mechanize.new
agent.get("http://www.google.com")
form = agent.page.forms.last
form.q = "stackoverflow"
a = form.submit.search
b = form.submit
puts a
=> [] # <--- EMPTY!
puts b
#<WWW::Mechanize::Page
{url
#<URI::HTTP:0x1020ea878 URL:http://www.google.co.uk/search?hl=en&source=hp&ie=ISO-8859-1&q=stackoverflow&meta=>}
{meta}
{title "stackoverflow - Google Search"}
{iframes}
{frames}
{links
#<WWW::Mechanize::Page::Link
"Images"
"http://images.google.co.uk/images?hl=en&source=hp&q=stackoverflow&um=1&ie=UTF-8&sa=N&tab=wi">
#<WWW::Mechanize::Page::Link
"Videos"
…
页面对象的搜索方法的行为类似于 Nokogiri 的搜索方法,因为它接受一系列 CSS 选择器和/或 XPath 查询并返回匹配元素的可枚举对象。例如
page.search('h3.r a.l', '//h3/a[@class="l"]')