ruby mechanize 需要完整的类名
为什么你需要 mechanize 的全名:
#!/usr/bin/ruby -w
require 'rubygems'
require 'pp'
require 'yaml'
require "mechanize"
yml = YAML.load_file 'login.yml'
user = yml["user"]
pword = yml["pword"]
a = WWW::Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
a.get('http://google.com/') do |page|
search_result = page.form_with(:name => 'f') do |search|
search.q = 'Hello world'
end.submit
search_result.links.each do |link|
puts link.text
end
end
当 mechanize example 不这样做时那?这是在上一个问题之上提出的。只有在阅读了有关该主题的上一个问题并添加完整的类(?)名称后,代码才有效。我在 Java 中似乎有点相似,但只有当它不明确时。在这里,没有任何含糊之处,只有机械化。
抱歉,之前的实际问题与上面的链接完全矛盾。我引用的上一个问题是这里 。重申一下,两个不同的问题,两个不同的答案。也许 API 或习惯用法发生了变化。
why do you need the full name for mechanize as so:
#!/usr/bin/ruby -w
require 'rubygems'
require 'pp'
require 'yaml'
require "mechanize"
yml = YAML.load_file 'login.yml'
user = yml["user"]
pword = yml["pword"]
a = WWW::Mechanize.new { |agent|
agent.user_agent_alias = 'Mac Safari'
}
a.get('http://google.com/') do |page|
search_result = page.form_with(:name => 'f') do |search|
search.q = 'Hello world'
end.submit
search_result.links.each do |link|
puts link.text
end
end
when the mechanize example doesn't do that? This is asked on top of a previous question. Code only worked after reading the previous question on this exact topic and adding the full class(?) name. I've seem somewhat similar in Java, but only when it's ambiguous. Here, there's nothing ambigious, there's only the one Mechanize.
Pardon, the actual previous question completely contradicts the above link. The previous question I was referencing is here. To reiterate, two different questions, two different answers. Maybe the API or idiom changed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的是哪个版本的 Mechanize?尝试
宝石列表机械化
。使用 Ruby 1.8.7-p357、1.9.2-p290 和 1.9.3-p0 以及 Mechanize 2.1,我能够实例化一个实例。例如:
并且:
我怀疑您正在使用 Mac OS,因为您正在
/usr/bin
访问 Ruby。默认情况下,Ruby 不会安装在 Windows 或 Linux 上,并且通常不会位于该路径中。Apple 版本的 Ruby 不包含 Mechanize,因此您在某个时候添加了它。因为 Apple 没有安装它,所以更新应该是良性的,所以也应该这样做:
Apple 确实在 Mac OS 上的应用程序中使用 Ruby,因此在更新其预安装的 gem 时必须注意这一点。
What version of Mechanize are you using? Try
gem list mechanize
.Using Ruby 1.8.7-p357, 1.9.2-p290, and 1.9.3-p0 and Mechanize 2.1 I am able to instantiate an instance. For instance:
and:
I suspect you are using Mac OS, because you are accessing Ruby at
/usr/bin
. Ruby is not installed by default on Windows or Linux and wouldn't be at that path normally.Apple's version of Ruby doesn't include Mechanize, so you added it at some point. Because Apple didn't install it it should be benign to update, so do:
Apple does use Ruby for apps on Mac OS, so you have to be aware of that when updating their pre-installed gems.