Fernando Maymone
Fernando Maymone

Reputation: 755

Conditional processing with Nokogiri

Im doing some scraping from web. And want that some data get saved, only if we find some tags inside it. My code looks like this:

def process
    doc = HTTParty.get("https://www.remotelyawesomejobs.com/jobs")
    @parse_page ||= Nokogiri::HTML(doc)
    @all_jobs = @parse_page.css(".job")
    @all_jobs.children.each do |job|
       @url =  @job_parse_page.at_css("h2 a")['href'] 
    end
end

The problem here. When Im doing

@url = job_parse_page.at_css("h2 a")['href'] 

Sometimes the children on the lasso doesnt have a css ("h2 a") and the app breaks and throws an error.

How is the best way to do "Hey. If you dont have a "h2 and a" on your DOM, just go to the next element? But if you have, do some other processing?

Upvotes: 0

Views: 135

Answers (2)

pguardiario
pguardiario

Reputation: 54984

You can do:

@url = job_parse_page.at_css("h2 a,[href]")['href'] rescue 'no href!'

That will look for the h2 a, then anything with a href attribute, and rescue with 'no href!'

Upvotes: 0

Casper
Casper

Reputation: 34308

Loops in Ruby can be advanced using the next keyword. So you could do something like this:

@all_jobs.each do |job|
  link = job.at_css("h2 a")
  next unless link

  @url = link['href']
  ... 
end

Upvotes: 1

Related Questions