Skip to content

Instantly share code, notes, and snippets.

@Mikke
Created October 4, 2012 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Mikke/3833207 to your computer and use it in GitHub Desktop.
Save Mikke/3833207 to your computer and use it in GitHub Desktop.
Добавление библиотеки Nokogiri в RubyOnRails проект
bundle install
rails g controller parser yandex
gem 'nokogiri'
class ParserController < ApplicationController
# для получения контента через http
require 'open-uri'
# подключаем Nokogiri
require 'nokogiri'
def yandex
end
end
class ParserController < ApplicationController
# для получения контента через http
require 'open-uri'
# подключаем Nokogiri
require 'nokogiri'
def yandex
source = 'http://catalog.yandex.ru/'
# получаем содержимое веб-страницы в объект
page = Nokogiri::HTML(open(source.to_s + page))
end
end
class ParserController < ApplicationController
# для получения контента через http
require 'open-uri'
# подключаем Nokogiri
require 'nokogiri'
def yandex
source = 'http://catalog.yandex.ru/'
# получаем содержимое веб-страницы в объект
page = Nokogiri::HTML(open(source.to_s + page))
# производим поиск по элементам с помощью css-выборки
page.css('a.b-rubric__list__item__link').each do |link|
data = Hash.new
data['text'] = link.content
data['href'] = link['href']
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment