开发者

How to insert a string to a text field using mechanize in ruby?

开发者 https://www.devze.com 2023-03-21 16:08 出处:网络
I know is a very simple question but I\'ve been stuck for an hour and I just can\'t understand how this works.

I know is a very simple question but I've been stuck for an hour and I just can't understand how this works.

I need to scrape some stuff from my school's library so I need to insert 'CE' to a text field and then click on a link with text 'Clasificación'. The output is what I am going to use to work. So here is my code.

require 'rubygems'
require 'open-uri'
require 'nokogiri'
require 'mechanize'

url = 'http://biblio02开发者_开发知识库.eld.edu.mx/janium-bin/busqueda_rapida.pl?Id=20110720161008#'
searchStr = 'CE'

agent = Mechanize.new
page = agent.get(url)

searchForm = page.form_with(:method => 'post')
searchForm['buscar'] = searchStr

clasificacionLink = page.link_with(:href => "javascript:onClick=set_index_and_submit(\'51\');").click
page = agent.submit(searchForm,clasificacionLink)

When I run it, it gives me this error

janium.rb:31: undefined method `[]=' for nil:NilClass (NoMethodError)

Thanks!


I think your problem is actually on line 13, not 31, and I'll even tell why I think that. Not only does your script not have 31 lines but, from the fine manual:

form_with(criteria)
Find a single form matching criteria.

There are several forms on that page that have method="post". Apparently Mechanize returns nil when it can't exactly match the form_with criteria including the single part mentioned in the documentation; so, if your criteria matches more than one thing, form_with returns nil instead of choosing one of the options and you end up trying to do this:

nil['buscar'] = searchStr

But nil doesn't have a []= method so you get your NoMethodError.

If you use this:

searchForm = page.form_with(:name => 'forma')

you'll get past the first part as there is exactly one form with name="forma" on that page. Then you'll have trouble with this:

clasificacionLink = page.link_with(:href => "javascript:onClick=set_index_and_submit(\'51\');").click
page = agent.submit(searchForm, clasificacionLink)

as Mechanize doesn't know what to do with JavaScript (at least mine doesn't). But if you use just this:

page = agent.submit(searchForm)

you'll get a page and then you can continue building and debugging your script.


mu's answer sounds reasonable. I am not sure if this is strictly necessary, but you might also try to put braces around searchStr.

searchForm['buscar'] = [searchStr]
0

精彩评论

暂无评论...
验证码 换一张
取 消