我正在使用RoR尝试使用 mechanize 在我的大学搜索一个简单的表单。该代码适用于搜索谷歌,但在结果中返回搜索表单?我真的很困惑。有什么建议?谢谢!
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
答案 0 :(得分:0)
我已经解决了!当调用form.submit
时,假设form.buttons
中的最后一个按钮是要使用的按钮。 form.buttons
中的最后一个按钮用于高级表单,因此生成的页面对象是另一种形式,尽管是更全面的高级搜索表单。
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"
result = agent.submit(form, form.buttons.first)
result.parser.css('table.cs-table-settings tr.tbl-class-fill-b td font b').map { |v| v.text.strip }
=> ["Principles of Chemistry", "Principles of Chemistry", "Principles of Chemistry", "Principles of Chemistry", …]
最后我们深究它! HTML非常糟糕,所以你需要为这个戴上你的XPath帽子! :)