i created mechanize action then i parse the page to store the data in sqlite then i am not getting what to do in controller and view to get bove results.this is following actions saved in folder in my app directory.
mechanize action
require 'rubygems'
require 'mechanize'
require 'fileutils'
DIR = 'data-hold/pages'
FileUtils.makedirs(DIR)
class GoogleController < ApplicationController
def index
def form_submit_w_exception_handling(frm)
retries = 3
begin
frm.submit(frm.button_with(:value=>'Search'))
rescue Exception=>e
puts "Problem: #{e}"
if retries < 0
retries -= 1
puts "Sleeping...#{retries} left"
retry
else
raise "Unexpected, repeated errors. Shutting down"
end
else
return frm
end
end
agent = Mechanize.new
agent.get(home_url)
form = agent.page.form_with(:action=>/AreaWiseSearch.aspx/)
form.field_with(:name=>select_field_names['selectcity']).options[1..-1].each do |selc_opt|
form[select_field_names['selectcity']] = selc_opt.value
#form.submit(form.button_with(:value=>'Search'))
form = form_submit_w_exception_handling(form)
puts "selectcity #{selc_opt.value}: #{agent.page.parser.css('tr').length}"
form.field_with(:name=>select_field_names['match']).options[1..-1].each do |mat_opt|
form[select_field_names['match']] = mat_opt.value
#form.submit(form.button_with(:value=>'Search'))
form = form_submit_w_exception_handling(form)
puts "match #{mat_opt.value}: #{agent.page.parser.css('tr').length}"
fname = "#{DIR}/#{selc_opt.value}--#{mat_opt.value}.html"
File.open(fname, 'w'){|f| f.puts agent.page.parser.to_html}
end
end
end
end
DBNAME = "data-hold/tel-directory.sqlite"
File.delete(DBNAME) if File.exists?DBNAME
DB = SQLite3::Database.new( DBNAME )
TABLE_NAME = "telephone_records"
DB_INSERT_STATEMENT = "INSERT into #{TABLE_NAME} values
(#{FIELD_NAMES.map{'?'}.join(',')})"
DB.execute "CREATE TABLE #{TABLE_NAME}(#{FIELD_NAMES.map{|f| "`#{f[0]}` #{f[1]}"}.join(', ')});"
FIELD_NAMES.each do |fn|
DB.execute "CREATE INDEX #{fn[2]} ON #{TABLE_NAME}(#{fn[0]})" unless fn[2].nil?
end
Dir.glob("data-hold/pages/*.html").reject{|f| f =~ /All match/}.each do |fname|
meta_info = File.basename(fname, '.html').split('--')
page = Nokogiri::HTML(open(fname))
Bookmarks