嗨,我设置了一个机械化任务来遍历一些清单并获取基本信息并保存到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
答案 0 :(得分:1)
是的,或者您可以一行完成:
tags = page.search(css).map{|el| el.text}