Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在ruby中解析nokogiri返回的一些结果,得到一条错误消息_Ruby_Nokogiri - Fatal编程技术网

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

在ruby中解析nokogiri返回的一些结果,得到一条错误消息,ruby,nokogiri,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 {

以下代码返回一个错误:

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
station\u names\u from\u map.rb:9:in'block in':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:在“每个块中”
from/opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node_set.rb:212:在'upto'中
from/opt/local/lib/ruby1.9/gems/1.9.1/gems/nokogiri-1.4.1/lib/nokogiri/xml/node_set.rb:212:在'each'中
从站名从地图。rb:7:in`'
壳返回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
str=''
reg=/href=“(.*)”title=“(.*)”/
href=reg.match(str)[1]
title=注册匹配(str)[2]
page=/.\/(.*).aspx$/.match(href)[1]
放置href
放置标题
放置页面
基本url=”http://www.amt.qc.ca"
完成\u url=base\u url+href
放置完整的url

有什么原因吗?

恰克在评论中指出,问题在于匹配:

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?

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

验证reg.match是否实际返回了某些内容。似乎不是。
|station| str = station.to_s