Is there any room for improvement on this code?
I use mechanize to get the links of a job listing web site. There are pages with pagination (when jobs > 25) and pages without.
If there is, then the link number 5 is labelled after "Next >" and link number 6 is "Last >>". In order to identify them, I make assertions on their name and act accordingly. I then check how many pages the pagination occurs, and then, taking the numbers, I scrape the pages one by one.
def scrape_duty_station
agent = Mechanize.new
page=agent.get(self.web_link)
if page.links[5].text =~ /^next/i
pagestart=page.links[5].href.match(/\d+/).to_s.to_i
pageend=page.links[6].href.match(/\d+/).to_s.to_i
@page=page.links[7...31]
scrape_single_page
(pagestart..pageend).each do |i|
@page=agent.get(self.web_link+"/#{i}")
@[email protected][7...31]
scrape_single_page
end
else
@page=page.links[5...30]
scrape_single_page
end
end
def scrape_single_page
@page.each do |i|
p=Position.new
p.title = i.text
p.web_link = i.href
p.job_id = i.href.match(/\d+/).to_s
p.station = Station.where(:web_link => self.web_link).first
p.save
end
end