嗨,我有一个机械化任务设置为循环通过一些列表,并获得基本信息,并保存到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发布于 2018-08-08 13:10:29
可以,也可以在一行代码中完成:
tags = page.search(css).map{|el| el.text}https://stackoverflow.com/questions/51716334
复制相似问题