解析nokogiri在ruby中返回的一些结果,收到错误消息

时间:2010-04-03 04:19:13

标签: ruby nokogiri

以下代码返回错误:

require 'nokogiri'
require 'open-uri'

@doc = Nokogiri::HTML(open("http://www.amt.qc.ca/train/deux-montagnes/deux-montagnes.aspx"))
#@doc = Nokogiri::HTML(File.open("deux-montagnes.html"))
stations =  @doc.xpath("//area")
stations.each { |station| str = station
    reg = /href="(.*)" title="(.*)"/
        href = reg.match(str)[1]
    title = reg.match(str)[2]
    page = /.*\/(.*).aspx$/.match(href)[1]
    puts href
    puts title
    puts page
    base_url = "http://www.amt.qc.ca"
    complete_url = base_url + href
    puts complete_url
}

ERROR:

station_names_from_map.rb:9:in `block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
        from /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node_set.rb:213:in `block in each'
        from /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node_set.rb:212:in `upto'
        from /opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node_set.rb:212:in `each'
        from station_names_from_map.rb:7:in `<main>'

shell returned 1

虽然此代码有效:

str = '<area shape="poly" alt="Deux-Montagnes" coords="59,108,61,106,65,106,67,108,67,113,65,115,61,115,59,113" href="/train/deux-montagnes/deux-montagnes.aspx" title="Deux-Montagnes">'

reg = /href="(.*)" title="(.*)"/
href = reg.match(str)[1]
title = reg.match(str)[2]
page = /.*\/(.*).aspx$/.match(href)[1]
puts href
puts title
puts page
base_url = "http://www.amt.qc.ca"
complete_url = base_url + href
puts complete_url

有什么理由?

2 个答案:

答案 0 :(得分:1)

正如查克在评论中指出的那样,问题就在于:

href = reg.match(str)[1]

它在周期的某个时刻被评估为:

nil[1]

并抛出错误,用以下内容更改您的匹配:

href_match = reg.match(str)
href = '' # ignore this line if you don't need a default value, but you're ok with a nil
href = href_match[1] unless href.nil?

答案 1 :(得分:0)

|station| str = station.to_s

这解决了这个问题,因为station实际上是一个Nokogiri :: XML :: Element实例。