```ruby
desc "Fetch merchant infos"
task :fetch_infos => :environment do
  require 'open-uri'
       
  url = "http://www.yp.com.hk/Medical-Beauty-Health-Care-Services-b/Beauty-Health/Beauty-Salons/p1/en/"   
  doc = Nokogiri::HTML(open(url))
  doc.css(".listing_div").each do |merchant|
    name = merchant.at_css(".cname").text
    address = merchant.at_css(".addr").text
    phone = merchant.at_css(".blacklink").text
    if merchant.at_css("div > .bluelink.overunder") != nil
      website = merchant.at_css("div > .bluelink.overunder").text 
    end 

    puts "#{name} - #{address} - #{phone} - #{website}"
  end
end```

Rewrite the nokogiri script to go from page to page
user
```ruby
desc "Fetch merchant infos"
task :fetch_infos => :environment do
  require 'open-uri'
  
  list = [ {url: "Beauty-Health/Beauty-Salons/p1/en/", name: Category.create(name: 'Beauty Salon')},
           {url: "Personal-Services/Massage/p1/en/",   name: Category.create(name: 'Massage')}]
  
  list.each do |category| 
    fetch_merchants(category[:url], category[:name]) 
  end  
end

def fetch_merchants(nextUrl, category)

  prefix = "http://www.yp.com.hk/Medical-Beauty-Health-Care-Services-b/"
  nextLinkText = "Next"

  while (!nextUrl.empty?)
    doc = Nokogiri::HTML(open(prefix+nextUrl))
    doc.css(".listing_div").each do |m|
     extract_merchant(m, category)
    end
    nextUrl = doc.xpath("//a[text()='#{nextLinkText}']/@href").first.to_s
    pause 1    
  end

end

def extract_merchant(merchant, category)
  m = Merchant.new
  m.name    = extract_css(merchant, '.cname')
  m.address = extract_css(merchant, '.addr')
  m.phone   = extract_css(merchant, '.blacklink')
  m.website = extract_css(merchant, 'div > .bluelink.overunder')
  m.category = category
  m.save

  puts "[MerchantSaved][#{}] #{m.name} - #{m.address} - #{m.phone} - #{m.website}"  
end

def extract_css(merchant, cssClass)
  return (merchant.at_css(cssClass) == nil) ? nil : merchant.at_css(cssClass).text 
end```
assistant