samedi 27 juin 2015

Try to get a possibly existing HTML tag

I'm using Nokogiri to parse some HTML:

  doc = Nokogiri::HTML(open(url))
  doc.css('.blockGroup--posts .block')[2..30].each do |article|
    title = article.at('h2').text # or h3, h4
    image = article.at('.section-content img')['src']  # may not exist

The problem is that sometimes the title may appear in h2 or h4 tags, so

title = article.at('h2').text

would report

undefined method `text' for nil:NilClass

The same goes for image. Sometimes there is no image in an article, and it will report an error too.

How can I make Nokogiri get these elements and continue running? Or can I use callbacks if these elements do not exist?

Aucun commentaire:

Enregistrer un commentaire