0

我创建了这个为图像解析 Wordpress XML 的方法。

 def get_image(doc, id)

    # Select all images in post
    images = Nokogiri::HTML(doc.xpath('//item')[id].xpath('content:encoded').text).xpath('//img')

    # Count number of images. 
    i = images.size

    if i == 0

      "No images found" 

    # We don't want icon images..
    elsif !images[0].attribute("src").to_s.scan("icon").empty?

      if i == 1
        "Only 1 picture that is icon - no image"
      else 

      t = 0

      #Loop all images and take the first that is not a smiley.
      loop do 
        t += 1
        images[t].attribute("src").to_s
        break if images[t].attribute("src").to_s.scan("icon").empty? || t > i
      end

      end 

    else

      images[0].attribute("src").to_s

    end

  end

此方法的目的是获取不是图标(笑脸)的图像。

它接受参数 doc,它是一个 Nokogiri XML 提要项目(博客文章)和 ID,它指的是项目编号。

如何在方法中创建循环?我希望它循环直到它达到 post(i) 中的总图像或属性不包含“图标”。

受 okliv 启发的更新解决方案:

  def get_image(doc, id)

    images = Nokogiri::HTML(doc.xpath('//item')[id].xpath('content:encoded').text).xpath('//img')

    images_not_icons = images.collect{|image| image.attribute("src").to_s unless !image.attribute("src").to_s.scan("icon").empty?}.compact

    if images_not_icons.empty? || images_not_icons.nil?
      "no image"
    else
      images_not_icons.first
    end

  end
4

1 回答 1

1

At the start you have an array of all images in images, right?

Then collect only not icons like this:

images_not_icons = images.select{|image| image.attribute("src").to_s.scan("icon").empty?}

And then loop them:

images_not_icons.each do |image|
  image.do_something
end

Or, if I understood correctly that you just need src urls, then it is even easier:

images_not_icons.collect{|image| image.attribute("src").to_s}

You even can grab it in one row:

urls_of_images_not_icons = images.collect{|image| image.attribute("src").to_s unless image.attribute("src").to_s.scan("icon").empty?}.compact
于 2013-07-08T13:38:13.610 回答