使用Nokogiri(json?)rake任务保存到数据库

时间:2015-06-15 19:07:31

标签: ruby-on-rails ruby json nokogiri

RoR noob在这里!我有一个rake任务做我想做的事我只是坚持如何将结果保存到我的language表。我希望此rake任务的结果填充language表上language字段的值。我愿意接受任何建议(比如在这里使用json)

namespace :scraper do
  desc "Scraper"
  task scrape: :environment do

require 'open-uri'
require 'nokogiri'
require 'csv'
require 'json'

url = "https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers"
page = Nokogiri::HTML(open(url))  
page.css('td b a').each do |line|
 puts line.text
end
end

1 个答案:

答案 0 :(得分:1)

namespace :scraper do
  desc "Scraper"
  task scrape: :environment do
    require 'open-uri'
    require 'nokogiri'
    require 'csv'
    require 'json'

    url = "https://en.wikipedia.org/wiki/List_of_languages_by_number_of_native_speakers"
    page = Nokogiri::HTML(open(url))  
    page.css('td b a').each do |line|
      puts line.text  # "Spanish" 
      Language.create(language: line.text)
    end
  end
end