#encoding:utf-8 require "open-uri" require "timeout" require ‘rubygems‘ require "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").text end
还有rest-client可以用来发送请求
时间: 2024-10-10 22:37:25