开发者

Parsing a complex URL in Ruby

I'd like to retrieve the value of 'q' in this URL:

http://www.google.com/url?sa=X&q=http://nashville.broadwayworld.com/article/Just_in_time_for_Halloween_Circle_Players_does_JEKYL开发者_StackOverflow社区L_HYDE_20101013&ct=ga&cad=:s7:f1:v1:d2:i0:lt:e0:p0:t1286988171:&cd=yQoOdKUFTLo&usg=AFQjCNEg2inHF8hXGEvG-TxMQyMx7YGHkA

if I use this:

uri = URI.parse("http://www.google.com/url?sa=X&q=http://nashville.broadwayworld.com/article/Just_in_time_for_Halloween_Circle_Players_does_JEKYLL_HYDE_20101013&ct=ga&cad=:s7:f1:v1:d2:i0:lt:e0:p0:t1286988171:&cd=yQoOdKUFTLo&usg=AFQjCNEg2inHF8hXGEvG-TxMQyMx7YGHkA")

uri_params = CGI.parse(uri.query)

uri_params['q']

I receive this error:

URI::InvalidURIError: bad URI(is not URI?)

Thanks!


seems to work for me

ruby-1.8.7-p249 > require 'uri'
 => true 
ruby-1.8.7-p249 > require 'cgi'
 => true 
ruby-1.8.7-p249 > uri = URI.parse("http://www.google.com/url?sa=X&q=http://nashville.broadwayworld.com/article/Just_in_time_for_Halloween_Circle_Players_does_JEKYLL_HYDE_20101013&ct=ga&cad=:s7:f1:v1:d2:i0:lt:e0:p0:t1286988171:&cd=yQoOdKUFTLo&usg=AFQjCNEg2inHF8hXGEvG-TxMQyMx7YGHkA")
 => #<URI::HTTP:0x10127b288 URL:http://www.google.com/url?sa=X&q=http://nashville.broadwayworld.com/article/Just_in_time_for_Halloween_Circle_Players_does_JEKYLL_HYDE_20101013&ct=ga&cad=:s7:f1:v1:d2:i0:lt:e0:p0:t1286988171:&cd=yQoOdKUFTLo&usg=AFQjCNEg2inHF8hXGEvG-TxMQyMx7YGHkA> 
ruby-1.8.7-p249 > uri_params = CGI.parse(uri.query)
 => {"cd"=>["yQoOdKUFTLo"], "sa"=>["X"], "cad"=>[":s7:f1:v1:d2:i0:lt:e0:p0:t1286988171:"], "ct"=>["ga"], "q"=>["http://nashville.broadwayworld.com/article/Just_in_time_for_Halloween_Circle_Players_does_JEKYLL_HYDE_20101013"], "usg"=>["AFQjCNEg2inHF8hXGEvG-TxMQyMx7YGHkA"]} 
ruby-1.8.7-p249 > uri_params['q']
 => ["http://nashville.broadwayworld.com/article/Just_in_time_for_Halloween_Circle_Players_does_JEKYLL_HYDE_20101013"] 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜