0

I made a parser(ruby file), which is based on Mechanize and put it in a model (I created model by rails generator with all attributes e.g name:string, email:string, etc and did a migration) in the rails app (whether correctly I did?). But I don't know how to make the parser record collected information to the database.

Parser:

class PrintingHouses < ActiveRecord::Base
require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.print-index.ru/default.aspx?p=82&gr=198')

loop do
  page.search('.goodname .goodname').each do |n|
    link = Mechanize::Page::Link.new(n, agent, page)
    new = link.click
    name = new.search('#ctl00_ctl00_BaseCentralRegion_CentralRegion_lblGoodName h1')
    address = new.search('.address')
    phone = new.search('.phone')
    email = new.search('.email')
    website = new.search('.www')
    puts name.text.center(100)
    print "Address: "
    puts address.text.strip
    print "Phone: "
    puts phone.text.strip
    print "email: "
    puts email.text.strip
    print "Website: "
    puts website.text.strip
  end
 break unless link = page.link_with(:text => " » " )
 page = link.click
 end
end
Arty
  • 15
  • 5
  • Which model do you want to parse to? What's the version of your Rails? – Bigxiang Aug 15 '13 at 09:27
  • What do you mean about model? I just want to save to the database parsed information but I don't know how to do it. Rails version is 4.0.0 – Arty Aug 15 '13 at 09:36
  • It means where do you want to save the data, PrintingHouses ? – Bigxiang Aug 15 '13 at 09:39
  • I want to save the data to the database through call method new or something like that in the model. Sorry if I understood wrong something – Arty Aug 15 '13 at 09:53

1 Answers1

0

I think I know your goal.

require 'mechanize'

agent = Mechanize.new
page = agent.get('http://www.print-index.ru/default.aspx?p=82&gr=198')

loop do
  page.search('.goodname .goodname').each do |n|
    link = Mechanize::Page::Link.new(n, agent, page)
    new = link.click
    name = new.search('#ctl00_ctl00_BaseCentralRegion_CentralRegion_lblGoodName h1')
    address = new.search('.address')
    phone = new.search('.phone')
    email = new.search('.email')
    website = new.search('.www')

    YourModelName.create(name: name.text.center(100), address: address.text.strip,
                         phone: phone.text.strip, email: email.text.strip, 
                         website: website.text.strip)


  end
 break unless link = page.link_with(:text => " » " )
 page = link.click
 end

just make sure your model name and column names are correct and no validation error.

Bigxiang
  • 6,252
  • 3
  • 22
  • 20