How to parse a HTML table with Nokogiri?

后端 未结 3 595
耶瑟儿~
耶瑟儿~ 2021-02-08 00:05

I\'m trying to parse a table but I don\'t know how to save the data from it. I want to save the data in each row row to look like:

[\'Raw name 1\', 2,094, 0,017,         


        
3条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-08 00:55

    I assume you were borrowing some code from here or any other related references (or I am sorry for adding wrong reference) - http://quabr.com/34781600/ruby-nokogiri-parse-html-table.

    However, if you want to capture all the rows, you can change the following codes -

    Hope this help you to solve your problem.

    doc = Nokogiri::HTML(open(html), nil, 'UTF-8')
    
    # We need .open tr, because we want to capture all the columns from a specific table's row
    
    @tablesArray = doc.css('table.open tr').reduce([]) do |array, row|
      # This will allow us to create result as this your illustrated one
      # ie. ['Raw name 1', 2,094, 0,017, 0,098, 0,113, 0,452]
      array << row.css('th, td').map(&:text)
    end
    
    render template: 'scrape_krasecology'
    

提交回复
热议问题