如何使用 ruby 中的 mechanize 将字符串插入到文本字段?
我知道这是一个非常简单的问题,但我已经被困了一个小时,我只是不明白这是如何工作的。
我需要从学校图书馆中抓取一些内容,因此需要将“CE”插入文本字段,然后单击包含文本“Clasificación”的链接。输出就是我将用来工作的内容。这是我的代码。
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)
当我运行它时,它给了我这个错误
janium.rb:31: undefined method `[]=' for nil:NilClass (NoMethodError)
谢谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你的问题实际上是在第 13 行,而不是第 31 行,我什至会告诉我为什么这么想。您的脚本不仅没有 31 行,而且来自 精美手册:
该页面上有多个表单具有
method="post"
。显然,当 Mechanize 无法完全匹配form_with
标准(包括文档中提到的 single 部分)时,它会返回nil
;因此,如果您的criteria
匹配多个内容,form_with
将返回nil
而不是选择其中一个选项,您最终会尝试执行此操作:但是
nil
没有[]=
方法,因此您会得到NoMethodError
。如果您使用它:
您将跳过第一部分,因为该页面上只有一个带有
name="forma"
的表单。那么你就会遇到麻烦:因为 Mechanize 不知道如何处理 JavaScript(至少我的不知道)。但如果您只使用这个:
您将获得一个
页面
,然后您可以继续构建和调试脚本。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:
There are several forms on that page that have
method="post"
. Apparently Mechanize returnsnil
when it can't exactly match theform_with
criteria including the single part mentioned in the documentation; so, if yourcriteria
matches more than one thing,form_with
returnsnil
instead of choosing one of the options and you end up trying to do this:But
nil
doesn't have a[]=
method so you get yourNoMethodError
.If you use this:
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:as Mechanize doesn't know what to do with JavaScript (at least mine doesn't). But if you use just this:
you'll get a
page
and then you can continue building and debugging your script.mu的回答听起来很有道理。我不确定这是否绝对必要,但您也可以尝试在
searchStr
周围加上大括号。mu's answer sounds reasonable. I am not sure if this is strictly necessary, but you might also try to put braces around
searchStr
.