InesM InesM - 1 year ago 40
Ruby Question

How to access only specific item instead of all page with nokogiri

I'm trying to scrape some information from a site using Nokogiri.

My code looks like this:

doc.css(".leftrightleft").each do |item|
title = item.at_css("h3 a").text
address = item.at_css(".biger+ p , h3+ p")'br').each do |n|
address = address.text

puts "-----"
puts title
puts "Address:"
puts address
puts "Course Types:"

item.css("table+ table tr+ tr").each do |type_price|
if type_price.css("td")[0] && type_price.css("td")[1] != nil
course_type = type_price.css("td")[0].text
course_type.gsub!(":", "")
price = type_price.css("td")[1].text
puts "\t #{course_type}: #{price}"

The problem is that when I do
item.css("table+ table tr+ tr").each do |type_price|
it's doing it for all items below that one and not only for that item in specific.

Can someone explain me what I'm doing wrong?

Answer Source

I've figured out a way to solve this.

Instead of making the loop like this item.css("table+ table tr+ tr").each do |type_price|, that makes it loop to the all page from item until the end of the page, I'm doing it like this item.at_css("table+ table tbody").css("tr+ tr").each.

With this solution I'm getting just the first table element and then I loop through the rest of the rows and it works.