我正在使用Mechanize从页面中提取链接。为了简化开发,我使用fakeweb来做超快的响应,以减少每次代码运行时的等待和烦人。
tags_url = "http://website.com/tags/"
FakeWeb.register_uri(:get, tags_url, :body => "tags.txt")
agent = WWW::Mechanize.new
page = agent.get(tags_url)
page.links.each do |link|
puts link.text.strip
end当我运行上面的代码时,它说:
nokogiri_test.rb:33: undefined method `links' for #<WWW::Mechanize::File:0x9a886e0> (NoMethodError)在检查页面对象的类之后
puts page.class # => File如果我不伪装tags_url,它就可以工作,因为页面类现在是Page类
puts page.class # => Page那么,我怎样才能使用带有mechanize的fakeweb来返回Page而不是File对象呢?
发布于 2009-12-09 18:38:05
使用FakeWeb播放预热HTTP请求:
tags_url = "http://website.com/tags/"
request = `curl -is #{tags_url}`
FakeWeb.register_uri(:get, tags_url, :response => request)
agent = WWW::Mechanize.new
page = agent.get(tags_url)
page.links.each do |link|
puts link.text.strip
end使用-i标志调用curl将在响应中包含报头。
发布于 2011-06-20 01:50:30
您可以很容易地解决这个问题,添加选项:content_type => "text/html" You your FakeWeb.register_uri call
https://stackoverflow.com/questions/1872428
复制相似问题