Skip to content

Instantly share code, notes, and snippets.

@Evanto
Last active May 8, 2017 20:59
Show Gist options
  • Save Evanto/581f0c43b972c39a13205e60515f64ba to your computer and use it in GitHub Desktop.
Save Evanto/581f0c43b972c39a13205e60515f64ba to your computer and use it in GitHub Desktop.
Code for seed.rb which seeds the db after rake db:seed (example):
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('https://www.learnathome.ru/blog/100-beautiful-words'))
#user = User.create(email: 'user@test.com', password: '123456788', password_confirmation: '123456788')
#block = Block.create(title: 'block 1', user: user)
doc.search('//table/tbody/tr').each do |row| # с каждым рядом указанной таблицы документа сделать следующее:
original = row.search('td[2]')[0].content.downcase # взять значение ячейки 2 каждого ряда
translated = row.search('td[4]')[0].content.downcase # взять значение ячейки 4 каждого ряда
Card.create!(original_text: original, translated_text: translated)
end
seed.rb example:
https://github.com/abuhtoyarov/flashcards/blob/master/db/seeds.rb#L8
Small example of using xpath:
http://www.chrisumbel.com/article/html_parsing_with_ruby_and_nokogiri.aspx
#nokogiri open-uri xpath
html table structure:
https://www.w3schools.com/tags/tag_td.asp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment