Rails 在循环中机械化循环

Rails Mechanize Loop within loop

你好,我设置了一个机械化任务来循环浏览一些列表并获取基本信息并保存到 csv

task :melrooftop => :environment do
  require 'mechanize'
  require 'csv'

  mechanize = Mechanize.new
  mechanize.history_added = Proc.new { sleep 10.0 }
  mechanize.ignore_bad_chunking = true
  mechanize.follow_meta_refresh = true
  page = mechanize.get('https://www.theurbanlist.com/melbourne/directory/search/eyJyZXN1bHRfcGFnZSI6Im1lbGJvdXJuZVwvZGlyZWN0b3J5XC9zZWFyY2giLCJrZXl3b3JkcyI6IlJvb2Z0b3AiLCJjYXRlZ29yeTpidXNpbmVzc190eXBlIjoiMiJ9')
  results = []
  results << ['name', 'streetAddress', 'addressLocality', 'postalCode', 'addressRegion', 'addressCountry', 'telephone', 'url', 'tags']

page.search('a[title="view business"]').each do |link|
    mechanize.click(link)

    name = mechanize.page.css('article h1[itemprop="name"]').text.strip
    streetAddress = mechanize.page.css('address span span[itemprop="streetAddress"]').text.strip
    addressLocality = mechanize.page.css('address span span[itemprop="addressLocality"]').text.strip
    postalCode = mechanize.page.css('address span span[itemprop="postalCode"]').text.strip
    addressRegion = mechanize.page.css('address span span[itemprop="addressRegion"]').text.strip
    addressCountry = mechanize.page.css('address span meta[itemprop="addressCountry"]').text.strip
    telephone = mechanize.page.css('address span[itemprop="telephone"]').text.strip
    url = mechanize.page.css('article p a[itemprop="url"]').text.strip
    results << [name, streetAddress, addressLocality, postalCode, addressRegion, addressCountry, telephone, url, tags]
  end

  CSV.open("melrooftop.csv", "w+") do |csv_file|
    results.each do |row|
      csv_file << row
    end
  end

在此循环中,我想收集标签列表并将它们添加到我的结果中。

我可以在我的循环中添加一个循环吗?

  tags = []
    tags = mechanize.page.css('div[class="column medium-3 __left-col-small"] article ul li').each do |tag|
      tag.text.strip
    end

是的,或者您可以在一行中完成:

tags = page.search(css).map{|el| el.text}