-1

あるサイトのニューアルバムのリリース情報をスクレイピングしようとしていて、Nokogiri 経由で処理しています。アイデアは、そのようなアイテムを含む素敵な配列を作成することです

[ 
  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

PSスクレイピングしようとしているページの形式は少し奇妙です:

<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>

これを 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>'

これを修正するにはどうすればよいですか?

4

2 に答える 2

1

あなたのソリューションに頭を悩ませることはできませんでしたが、少し遊んだ後、これを思いつきました。

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

出力:

[{:album=>"Some album", :artist=>"Jean Luc-Ponty", :date=>"20 October 1989"},
{:album=>"Some album", :artist=>"Some Other Artist", :date=>"20 October 1989"},
{:album=>"Some album", :artist=>"Some Other Artist", :date=>"20 October 1989"},
{:album=>"Some album", :artist=>"Some Other Artist", :date=>"29 October 1989"}]

あなたが求めていたものとは正確には異なりますが、おそらくもう少しRubyの慣用句であり、必要に応じて変更できると確信しています.

于 2012-10-30T15:37:57.537 に答える
-1

index 変数は、2 番目の では定義されていませんeach

于 2012-10-30T12:57:19.897 に答える