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

333 views Asked by At

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
1

There are 1 answers

0
Ravindra On BEST ANSWER
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