Created
October 4, 2012 12:12
-
-
Save Mikke/3833207 to your computer and use it in GitHub Desktop.
Добавление библиотеки Nokogiri в RubyOnRails проект
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bundle install |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
rails g controller parser yandex |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'nokogiri' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ParserController < ApplicationController | |
# для получения контента через http | |
require 'open-uri' | |
# подключаем Nokogiri | |
require 'nokogiri' | |
def yandex | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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