首页 > 代码库 > Ruby之发送请求获取数据

Ruby之发送请求获取数据

#encoding:utf-8require "open-uri"require "timeout"require rubygemsrequire "nokogiri"uri = "http://api.map.baidu.com/direction/v1?mode=driving&origin=&destination=北京大学&origin_region=北京&destination_region=北京&output=xml&ak=E4805d16520de693a3fe707cdc962045上地五街"begin    req = open(URI.escape(uri)).read    doc = Nokogiri::XML(open(URI.escape(uri)), nil, utf-8)    # doc.encoding = "utf-8"    root = doc.root    origin = root.xpath("result/origin/originPt")    rescue StandardError,Timeout::Error, SystemCallError,Errno::ECONNREFUSED    puts $!else    puts origin.xpath("lng").textend

 还有rest-client可以用来发送请求

Ruby之发送请求获取数据