代码之家  ›  专栏  ›  技术社区  ›  Khat

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

  •  0
  • Khat  · 技术社区  · 16 年前

    以下代码返回错误:

    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
    }
    

    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 回复  |  直到 16 年前
        1
  •  1
  •   makevoid    16 年前

    正如Chuck在评论中指出的那样,问题在于匹配:

    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?
    
        2
  •  0
  •   Khat    16 年前
    |station| str = station.to_s
    

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