Question

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
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top