0

我基本上是想从我对 ChartLyrics API 的响应中获取 Lyric 标签。这是我写的代码:

require 'nokogiri'
require 'open-uri'

request = Net::HTTP.get(URI.parse('http://api.chartlyrics.com/apiv1.asmx/GetLyric?lyricId=1710&lyricCheckSum=a4a56a99ee00cd8e67872a7764d6f9c6'))
puts request
response = Nokogiri::XML(request)

puts response.xpath("//Lyric")[0].to_s

我已阅读文档,但没有找到答案。我在这里做错了什么?

4

2 回答 2

1

试试下面的代码

require 'open-uri'
require 'nokogiri'
xml_doc = Nokogiri::XML(open('http://api.chartlyrics.com/apiv1.asmx/GetLyric?lyricId=1710&lyricCheckSum=a4a56a99ee00cd8e67872a7764d6f9c6'))
#I always prefer css than xpath
lyrics = xml_doc.css('Lyric')
if lyrics.empty?
  puts "COuld not find any lyric in the XML document'
else
  puts lyrics[0].to_s
end
于 2012-10-17T18:52:40.073 回答
1

做 response.remove_namespaces!在 response.xpath 之前

于 2012-10-17T18:54:18.163 回答