0

我是一個業餘程序員想從一個網站,類似於本網站抽取數據:http://www.highschoolsports.net/massey/(我有權刮網站,順便說一句。)從表中的類名創建動態變量,將td值移動到該行的數組或散列中?

目標網站有「日」班但是我想確保每個表中的每個「TD」都以某種方式鏈接到該類的名稱,因爲這些表是不一致的,例如一個表可能是:

行[0] - >> th.name,th.place,th.team

行[1] - >> TD [0],TD [1],TD [2]

而另一個可能是:

行[0] - >> th.place,th.team,th.name

行[1] - >> TD [0],TD [1],TD [2]等等。

我的問題:如何在跨越數百個不一致的表格(以'th'類順序)捕獲'th'類名並創建10-14個變量(數組)然後將表中與該列相對應的'td'鏈接到該動態變量?請讓我知道這是令人困惑的。還有一個給定頁面上的多個表..

目前我的代碼是一樣的東西:

require 'rubygems' 
require 'mechanize' 
require 'nokogiri' 
require 'uri' 

class Result 

    def initialize(row) 
    @attrs = {} 
    @attrs[:raw] = row.text 
    end 

end 

class Race 

    def initialize(page, table) 
    @handle = page 
    @table = table 
    @results = [] 
    @attrs = {} 
    parse! 
    end 

    def parse! 
    @attrs[:name] = @handle.css('div.caption').text 
    get_rows 

    end 

    def get_rows 
    # get all of the rows .. 
    @handle.css('tr').each do |tr| 
     @results << RaceResult.new(tr) 
    end 
    end 

end 

class Event 

    class << self 

    def all(index_url) 
     events = [] 
     ourl = Nokogiri::HTML(open(index_url)) 
     ourl.css('a.event').each do |url| 
     abs_url = MAIN + url.attributes["href"] 
     events << Event.new(abs_url) 
     end 
     events 
    end 

    end 

    def initialize(url) 
    @url = url 
    @handle = nil 
    @attrs = {} 
    @races = [] 
    @sub_events = [] 
    parse! 
    end 

    def parse! 
    @handle = Nokogiri::HTML(open(@url)) 
    get_page_meta 
    if(@handle.css('table.base.event_results').length > 0) 
     @handle.search('div.table_container.event_results').each do |table| 
     @races << Race.new(@handle, table) 
     end 
    else 
     @handle.css('div.centered a.obvious').each do |ol| 
     @sub_events << Event.new(BASE_URL + ol.attributes["href"]) 
     end 
    end 
    end 

    def get_page_meta 
    @attrs[:name] = @handle.css('html body div.content h2 text()')[0] # event name 
    @attrs[:date] = @handle.xpath("html/body/div/div/text()[2]").text.strip #date 
    end 

end 

一個朋友一直在幫助我這個和我剛剛開始掌握OOP,但我只捕獲表,他們不分割成td和存儲到某種變量/數組/哈希等。我需要幫助理解這個過程或如何做到這一點。關鍵部分將根據數據的類別動態分配變量名稱,並將該列中的'td'(例如所有td [2])移動到該動態變量名稱中。我不能告訴你,如果有人真的可以幫助我解決這個問題並理解如何完成這項工作,那將會是多麼美妙。預先感謝您的任何幫助!

回答

0

很容易,一旦你意識到第四個內容是你的哈希鍵。例如:

@items = [] 
doc.css('table.masseyStyleTable').each do |table| 
    fields = table.css('th').map{|x| x.text.strip} 
    table.css('tr').each do |tr| 
     item = {} 
     fields.each_with_index do |field,i| 
      item[field] = tr.css('td')[i].text.strip rescue '' 
     end 
     @items << item  
    end 
end 
+0

pguardiario, 你太棒了!我想出了以下我的自我: ' 高清get_results 行數= [] 行= @ table.css( 'TR') 頭= rows.shift 提出@attrs [:theads] = header.text rows = rows.map do | row | _row = {} header.each_with_index do | h,i | 結束 _row 結束 end' 我不知道現在該怎麼查看此,也許$ log.info 「{#@項目[項目]}」 ? 你將如何卸載到DataMapper?我真的很感激它,你太棒了!再次感謝! – user1010100

+0

我不確定DM,但只要字段與您的密鑰匹配,您只需執行MyTable.new(item).save - 不要忘記接受我的回答 – pguardiario

+0

非常感謝!超過接受! – user1010100