user3735114 user3735114 - 3 months ago 6x
Ruby Question

Complex loop with Nokogiri

I'm struggling to return results using this:

url = ""

doc = Nokogiri::HTML(open(url))
doc.css(".location").each do |location|
puts location.at_css(".city_odd, city_even").text

This was just trying to get results from the URL above. I can change it to just return one result with:

url = ""
doc = Nokogiri::HTML(open(url))
puts doc.at_css(".city_odd").text

but I don't understand how to iterate through all the results especially as theres a
CSS tag that I need to add in there. If I insert this as well it just gets ignored.

Furthermore I want to remove the "- Serviced by MOLLY MAID Aberdeen" text from the results. Each line of data appears as "Altens Ind Estate - Serviced by MOLLY MAID Aberdeen". Would
be correct here?

In an ideal world I would like to automatically go through all of this list doing the same though I am unsure if this is possible?


I would use xpath instead as it's a little more expressive. Here's a working example:

url = ""

doc = Nokogiri::HTML(open(url))
doc.xpath("//div[@class='city_even' or @class='city_odd']/a/text()").each do |location|
    puts location

If you read the xpath expression out loud, it means: find all divs in the document who have a class attribute equal to 'city_even' or 'city_odd', then find all anchor tags who are children of those divs, then find all text nodes who are children of those anchor tags.

When Nokogiri returns with a NodeSet containing the city names, this code iterates through each city name and puts it.