Search code examples
ruby-on-railsruby-on-rails-4web-scrapingnokogiriscreen-scraping

using while loop with nokogiri gem to navigate to next page


Here is my code for next page

require 'nokogiri'
require 'open-uri'

doc = Nokogiri::HTML(open("http://www.asklaila.com/search/Pune/-/Electrician/10?searchNearby=false&v=listing"))
 c=  doc.css('.resultTitle').collect {|node| node.text.strip}

while a = doc.at('a.btnNextPre')
   doc = Nokogiri::HTML(open(a[:href]))
    c=  doc.css('.resultTitle').collect {|node| node.text.strip}
end

I want to scrape the data of all pages.But I am getting data of first page only.I am not getting data of other pages.Can anybody help me? Thanks in advance


Solution

  • Your code not targetting to proper class. In your while loop your are targetting to a.btnNextPre but class btnNextPre belongs to li tag so it should be li.btnNextPre. So try change your code something like:

    require 'nokogiri'
    require 'open-uri'
    
    c = []
    doc = Nokogiri::HTML(open("http://www.asklaila.com/search/Pune/-/Electrician?searchNearby=false&v=listing"))
     c.push(doc.css('.resultTitle').collect {|node| node.text.strip})
    
    while a = doc.at('li.btnNextPre a')
      doc = Nokogiri::HTML(open(a[:href]))
      c.push(doc.css('.resultTitle').collect {|node| node.text.strip})
    end
    
    c