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