首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Rails机械化循环中的循环

Rails机械化循环中的循环
EN

Stack Overflow用户
提问于 2018-08-07 06:14:08
回答 1查看 77关注 0票数 1

嗨,我有一个机械化任务设置为循环通过一些列表,并获得基本信息,并保存到csv

代码语言:javascript
复制
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

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

我能用下面的代码在我的循环中添加一个循环吗?

代码语言:javascript
复制
  tags = []
    tags = mechanize.page.css('div[class="column medium-3 __left-col-small"] article ul li').each do |tag|
      tag.text.strip
    end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-08 13:10:29

可以,也可以在一行代码中完成:

代码语言:javascript
复制
tags = page.search(css).map{|el| el.text}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51716334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档