在Ruby中满足条件时强制Mechanize返回HTTP Success
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Ruby中满足条件时强制Mechanize返回HTTP Success相关的知识,希望对你有一定的参考价值。
require 'rubygems' require 'uri' require 'mechanize' require 'rack/utils' # so you can do `params.inspect` throughout Mechanize class NilClass; def search(*args); []; end; end Mechanize::Chain::PostConnectHook.class_eval do def handle(ctx, params) headers = params[:response].to_hash if headers.has_key?("location") headers["location"].each do |location| url = URI.parse(location) if url.host == "localhost" && url.port == 4567 params[:res_klass] = Net::HTTPSuccess end end end p params.keys #=> [:res_klass, :uri, :response_body, :referer, :response, :agent, :verb, :redirects, :connection, :params, :request, :headers] p params[:uri] #=> #<URI::HTTPS:0x1aa0810 URL:https://somesite.com?key=value> p params[:response] #=> #<Net::HTTPFound 302 Found readbody=true> p params[:response].to_hash #=> {"location"=>["http://somesite.com"], "expires"=>["Sat, 01 Jan 2000 00:00:00 GMT"], "content-type"=>["text/html; charset=utf-8"], "date"=>["Mon, 16 Aug 2010 04:31:13 GMT"], "content-length"=>["0"], "cache-control"=>["private, no-cache, no-store, must-revalidate"], "x-cnection"=>["close"], "pragma"=>["no-cache"]} super(ctx, params) end end agent = Mechanize.new page = agent.get("https://somesite.com") # ... tons of redirects, # then finally we find a match in our `handle` method above, # and reset the `params[:res_klass]` to `Net::HTTPSuccess`. # that is a hack to say basically, # "return the page, we found what we want" location = URI.parse(page.response.to_hash["location"].to_a.first) params = Rack::Utils.parse_query(location.query)
以上是关于在Ruby中满足条件时强制Mechanize返回HTTP Success的主要内容,如果未能解决你的问题,请参考以下文章
ruby 使用CLI的Magento DB修复工具 - Ruby + Mechanize
ruby 使用docs提供的代码测试Mechanize gem
ruby 使用带有Mechanize的Pismo gem从网页中提取内容。来自http://stackoverflow.com/questions/14283974/what-ruby-gem-pro