简体   繁体   中英

Saving to a database using a Nokogiri (json?) rake task

RoR noob here! I have a rake task doing what I want to do I am just stuck on how to get the results saved to my language table. I want the results from this rake task to populate the values of the language field on my language table. I'm open to any suggestions (like using json here)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM