小编ver*_*rom的帖子

如何用Nokogiri解析HTML表?

我正在尝试解析一个表,但我不知道如何从中保存数据.我想将每行中的数据保存为:

['Raw name 1', 2,094, 0,017, 0,098, 0,113, 0,452]
Run Code Online (Sandbox Code Playgroud)

样本表是:

html = <<EOT
    <table class="open">
        <tr>
            <th>Table name</th>
            <th>Column name 1</th>
            <th>Column name 2</th>
            <th>Column name 3</th>
            <th>Column name 4</th>
            <th>Column name 5</th>
        </tr>
        <tr>
            <th>Raw name 1</th>
            <td>2,094</td>
            <td>0,017</td>
            <td>0,098</td>
            <td>0,113</td>
            <td>0,452</td>         
        </tr>
        .
        .
        .
        <tr>
            <th>Raw name 5</th>
            <td>2,094</td>
            <td>0,017</td>
            <td>0,098</td>
            <td>0,113</td>
            <td>0,452</td>         
        </tr>
    </table>
EOT
Run Code Online (Sandbox Code Playgroud)

我的刮刀代码是:

  doc = Nokogiri::HTML(open(html), nil, 'UTF-8')
  tables = doc.css('div.open')

  @tablesArray = []

  tables.each do |table|
    title = table.css('tr[1] > th').text
    cell_data …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails html-parsing nokogiri

10
推荐指数
1
解决办法
5296
查看次数

## <Class:0xb4107fc0>的未定义方法`articles_path':0xb498bf48>

我在fle new.html.erb中遇到了pails应用程序的问题:

<%= form_for @article do |f| %>
   <%= f.text_field :title %>
   <%= f.text_area :text %>
   <%= f.submit %>
<% end %>
Run Code Online (Sandbox Code Playgroud)

我的routes.rb有:

  resources :atricles
Run Code Online (Sandbox Code Playgroud)

我的static_pages_controller.rb有代码:

  def manager
    @contact_messages = ContactForm.all
    @item = Item.new
    @items = Item.all
    @article = Article.new
  end
Run Code Online (Sandbox Code Playgroud)

我的articles_controller.rb是:

class ArticlesController < ApplicationController
    def new
       @article = Article.new
    end
    def create
       @article = Article.new article_params
       @article.save
    end

    private

    def article_params
       params.require(:article).permit(:title, :text)
    end
end
Run Code Online (Sandbox Code Playgroud)

我的迁移文件是:

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title …
Run Code Online (Sandbox Code Playgroud)

ruby ruby-on-rails

1
推荐指数
1
解决办法
256
查看次数

标签 统计

ruby ×2

ruby-on-rails ×2

html-parsing ×1

nokogiri ×1