王筝的博客
ruby学习

 

https://github.com/rest-client/rest-client

1) Add new gem to Gemfile, then bundle install

gem ‘rest-client’, ‘~> 2.0.2′

 

2) Use rails console to test new gem

irb(main):001:0> url = “http://www.weather.com.cn/data/sk/101190408.html”

=> “http://www.weather.com.cn/data/sk/101190408.html”

irb(main):002:0> safeurl = URI.encode(url.strip)

=> “http://www.weather.com.cn/data/sk/101190408.html”

irb(main):003:0> response = RestClient.get(safeurl)

=> <RestClient::Response 200 “{\”weatherin…”>

irb(main):004:0> url_hash = JSON.parse(response.body)

=> {“weatherinfo”=>{“city”=>”太仓”, “cityid”=>”101190408″, “temp”=>”22.8″, “WD”=>”东风”, “WS”=>”小于3级”, “SD”=>”81%”, “AP”=>”1005.5hPa”, “njd”=>”暂无实况”, “WSE”=>”<3″, “time”=>”17:55″, “sm”=>”3.2″, “isRadar”=>”0″, “Radar”=>””}}

irb(main):005:0> response.code

=> 200

Leave a Reply