-2

I currently have a scraper file called scraper.rb. I need to figure out how to take the output from this and have it display on a Sinatra server. If you could also provide an explanation of why your answer works that would be great, thanks in advance.

require 'httparty'
require 'nokogiri'


url = "https://miami.craigslist.org/search/sof"


response = HTTParty.get url

puts response.body
puts response.headers['content-type']


dom = Nokogiri::HTML(response.body)

num = 0

dom.css("a.hdrlnk").each do |job|
num +=1
print "#{num} "
puts job.content
puts job['href']
end
HiStakes
  • 17
  • 1
  • 1
    There are so many solutions to your problem, you can use just a caching variable and save your results there, you can save it in files or in a real SQL/noSQL database. – Sir l33tname Jul 20 '17 at 07:59

1 Answers1

0

I don't know the structure of your app, following is working for me. Hope it helps you.

require 'sinatra'

get '/' do 
  @data = get_craig_data
  erb :index   
end


private 
  def get_craig_data
    require 'httparty'
    require 'nokogiri'

    url = "https://miami.craigslist.org/search/sof"
    data = [] # array to be returned
    response = HTTParty.get url
    dom = Nokogiri::HTML(response.body)
    num = 0A little more detail would 
    dom.css("a.hdrlnk").each do |job|
      num +=1
      data.push({num: "#{num}", content: job.content, link: job['href']})
    end
    data
  end#method end
__END__


@@index
  <table>
   <thead>
     <tr>
       <th>Num#</th><th>Job Content</th><th>Link</th>
     </tr>
   </thead>
   <tbody>
    <%@data.count.times do |i|%>
      <tr>
        <td><%=@data[i][:num]%></td>
        <td><%=@data[i][:content]%></td>
        <td><%=@data[i][:link]%></td>
      </tr>
    <%end%>
    </tbody>
  </table>
Sumeet Masih
  • 597
  • 1
  • 8
  • 22