Ruby-写入嵌套数组
我正试图搜集一个网站新专辑发行的信息,我正在通过Nokogiri处理这个问题。我们的想法是创建一个很好的数组,其中包含这样的项Ruby-写入嵌套数组,ruby,web-scraping,nokogiri,Ruby,Web Scraping,Nokogiri,我正试图搜集一个网站新专辑发行的信息,我正在通过Nokogiri处理这个问题。我们的想法是创建一个很好的数组,其中包含这样的项 [ 0 => ['The Wall', 'Pink Floyd', '1979'], 1 => ['Led Zeppelin I', 'Led Zeppelin', '1969'] ] 这是我当前的代码。我是一个十足的红宝石新手,所以任何建议都将不胜感激 @events = Array.new() # for every date we en
[
0 => ['The Wall', 'Pink Floyd', '1979'],
1 => ['Led Zeppelin I', 'Led Zeppelin', '1969']
]
这是我当前的代码。我是一个十足的红宝石新手,所以任何建议都将不胜感激
@events = Array.new()
# for every date we encounter
doc.css("#main .head_type_1").each do |item|
date = item.text
# get every albumtitle
doc.css(".albumTitle").each_with_index do |album, index|
album = album.text
@events[index]['album'] = album
@events[index]['release_date'] = date
end
#get every artistname
doc.css(".artistName").each do |artist|
artist = artist.text
@events[index]['artist'] = artist
end
end
puts @events
顺便说一句,我正在尝试的页面格式有点奇怪:
<tr><th class="head_type_1">20 October 1989</th></tr>
<tr><td class="artistName">Jean Luc-Ponty</td><td class="albumTitle">Some example album</td></tr>
<tr><td class="artistName">Some Other Artist</td><td class="albumTitle">Some example album</td></tr>
<tr><td class="artistName">Some Other Artist</td><td class="albumTitle">Some example album</td></tr>
<tr><th class="head_type_1">29 October 1989</th></tr>
<tr><td class="artistName">Some Other Artist</td><td class="albumTitle">Some example album</td></tr>
1989年10月20日
Jean-Luc PontySome示例专辑
一些其他艺术家一些例子专辑
一些其他艺术家一些例子专辑
1989年10月29日
一些其他艺术家一些例子专辑
当我尝试在ruby解释器中运行此命令时,会出现以下错误:
get_events.rb:25:in `block (2 levels) in <main>': undefined method `[]=' for nil:NilClass (NoMethodError)
from /Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `each'
from get_events.rb:23:in `each_with_index'
from get_events.rb:23:in `block in <main>'
from /Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:239:in `block in each'
from /Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `upto'
from /Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in `each'
from get_events.rb:18:in `<main>'
get_events.rb:25:in`block(2层)in':nil:NilClass(NoMethodError)的未定义方法“[]”
from/Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:239:in“每个块中”
from/Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in'upto'
from/Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in'each'
来自get_事件。rb:23:in'each_with_index'
来自get_事件。rb:23:in'block in'
from/Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:239:in“每个块中”
from/Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in'upto'
from/Users/adrian/.rvm/gems/ruby-1.9.3-p286/gems/nokogiri-1.5.5/lib/nokogiri/xml/node_set.rb:238:in'each'
来自get_事件。rb:18:in`'
如何解决此问题?第二个
中的索引变量未定义,每个我无法理解您的解决方案,但在玩了一会儿之后,我想到了这个
require 'pp'
require 'nokogiri'
str = %Q{
<tr><th class="head_type_1">20 October 1989</th></tr>
<tr><td class="artistName">Jean Luc-Ponty</td><td class="albumTitle">Some album</td></tr>
<tr><td class="artistName">Some Other Artist</td><td class="albumTitle">Some album</td></tr>
<tr><td class="artistName">Some Other Artist</td><td class="albumTitle">Some album</td></tr>
<tr><th class="head_type_1">29 October 1989</th></tr>
<tr><td class="artistName">Some Other Artist</td><td class="albumTitle">Some album</td></tr>
}
doc = Nokogiri::HTML(str)
date = ""
result = []
doc.xpath("//tr").each do |tr|
children = tr.children
if children.first["class"] == "head_type_1"
date = children.first.content
else
artist, album = children.map {|c| c.content}
result << {album: album, artist: artist, date: date}
end
end
pp result
require'pp'
需要“nokogiri”
str=%Q{
1989年10月20日
Jean-Luc PontySome专辑
一些其他艺术家一些专辑
一些其他艺术家一些专辑
1989年10月29日
一些其他艺术家一些专辑
}
doc=Nokogiri::HTML(str)
date=“”
结果=[]
doc.xpath(“//tr”)。每个都做| tr|
儿童
如果是儿童。第一个[“类”]=“头部类型1”
日期=children.first.content
其他的
艺术家,专辑=children.map{| c | c.content}
结果“一些专辑”;:艺术家=>“Jean-Luc Ponty”;:日期=>“1989年10月20日”},
{:album=>“一些专辑”,“艺术家=>“一些其他艺术家”,“日期=>“1989年10月20日”},
{:album=>“一些专辑”,“艺术家=>“一些其他艺术家”,“日期=>“1989年10月20日”},
{:album=>“一些专辑”,“艺术家=>“一些其他艺术家”,“日期=>“1989年10月29日”}]
这不完全是您想要的,但可能更像Ruby惯用语,我相信您可以根据需要修改它。添加了错误输出和问题:)当您为如此复杂的代码添加错误消息时,您应该将行号添加到代码中。你认为有人会通过代码为你做所有的工作吗?这难道不会让复制粘贴变得不可能吗?不是这样,我已经尝试了doc.css(“artistName”)。每个带有索引的U都是艺术家,索引-相同的输出这正是我试图实现的,并且几乎达到了目的,但有一个小错误。非常感谢您的关注!代码的潜台词是,除了让-吕克-庞蒂之外,没有其他艺术家。:-)