如何使用 ruby​​ 中的 mechanize 将字符串插入到文本字段?

发布于 2024-11-25 08:40:54 字数 760 浏览 2 评论 0原文

我知道这是一个非常简单的问题,但我已经被困了一个小时,我只是不明白这是如何工作的。

我需要从学校图书馆中抓取一些内容,因此需要将“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你在看孤独的风景 2024-12-02 08:40:54

我认为你的问题实际上是在第 13 行,而不是第 31 行,我什至会告诉我为什么这么想。您的脚本不仅没有 31 行,而且来自 精美手册

form_with(条件)
查找单个表单匹配条件。

该页面上有多个表单具有 method="post"。显然,当 Mechanize 无法完全匹配 form_with 标准(包括文档中提到的 single 部分)时,它会返回 nil ;因此,如果您的 criteria 匹配多个内容,form_with 将返回 nil 而不是选择其中一个选项,您最终会尝试执行此操作:

nil['buscar'] = searchStr

但是 nil 没有 []= 方法,因此您会得到 NoMethodError

如果您使用它:

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

您将跳过第一部分,因为该页面上只有一个带有 name="forma" 的表单。那么你就会遇到麻烦:

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

因为 Mechanize 不知道如何处理 JavaScript(至少我的不知道)。但如果您只使用这个:

page = agent.submit(searchForm)

您将获得一个页面,然后您可以继续构建和调试脚本。

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.

压抑⊿情绪 2024-12-02 08:40:54

mu的回答听起来很有道理。我不确定这是否绝对必要,但您也可以尝试在 searchStr 周围加上大括号。

searchForm['buscar'] = [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.

searchForm['buscar'] = [searchStr]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文